gpt4 book ai didi

go - Response.Body 返回 "empty"正文

转载 作者:IT王子 更新时间:2023-10-29 01:40:56 25 4
gpt4 key购买 nike

我收到了回复以及标题回复和所有内容,但由于某种原因正文是空的:

https://repl.it/repls/HastyAggravatingArchitect

package main

import (
"fmt"
"net/http"
"io/ioutil"
)


const url = "http://comicbookdb.com/search.php"

func main() {
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println(err.Error())
return
}
q := request.URL.Query()
q.Add("form_search", "Captain America")
q.Add("form_searchtype", "Character")
// http://comicbookdb.com/search.php?form_search=captain%20america&form_searchtype=Character
request.URL.RawQuery = q.Encode()
client := http.DefaultClient
response, err := client.Do(request)
fmt.Println(response.Header.Get("Date"))
fmt.Println(response.StatusCode)
fmt.Println(response.Header.Get("Server"))
fmt.Println(response.Body)

r, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(r))
}

我在完整的URL中进行了注释,以便您自己查看并查看响应正文不应为空。

最佳答案

问题不在 go 程序中,您需要提供额外的 header ,在这种情况下它是一个 Cookie header :

package main

import (
"fmt"
"io/ioutil"
"net/http"
)

const url = "http://comicbookdb.com/search.php"

func main() {
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println(err.Error())
return
}
// Here is requeired header
request.Header.Add("Cookie", "PHPSESSID=jmujtqjctuk1bv1g02ni88q9u5'")
q := request.URL.Query()
q.Add("form_search", "Captain America")
q.Add("form_searchtype", "Character")
request.URL.RawQuery = q.Encode()
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(response.Header.Get("Date"))
fmt.Println(response.StatusCode)
fmt.Println(response.Header.Get("Server"))
fmt.Println(response.Body)

r, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(r))
}

输出:

Mon, 09 Jul 2018 06:13:35 GMT
200
Apache
&{0xc420060040 {0 0} false <nil> 0x5e2200 0x5e2190}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org ...
..... omitted ....

关于go - Response.Body 返回 "empty"正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51238163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com