Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cURL和Golang POST-Can的不同反应';我不明白为什么_Curl_Go - Fatal编程技术网

cURL和Golang POST-Can的不同反应';我不明白为什么

cURL和Golang POST-Can的不同反应';我不明白为什么,curl,go,Curl,Go,我正在尝试使用golang的http客户端从服务器获取响应 我希望通过go执行的请求应该与以下curl命令相同: curl --data "fulladdress=22280+S+209th+Way%2C+Queen+Creek%2C+AZ+85142" 'http://www.homefacts.com/hfreport.html' 我已经编写了等效的go代码,还尝试使用一个名为的服务,该服务为上述curl请求生成以下go代码: // Generated by curl-to-Go:

我正在尝试使用golang的
http
客户端从服务器获取响应

我希望通过go执行的请求应该与以下
curl
命令相同:

curl  --data "fulladdress=22280+S+209th+Way%2C+Queen+Creek%2C+AZ+85142"  'http://www.homefacts.com/hfreport.html'
我已经编写了等效的go代码,还尝试使用一个名为的服务,该服务为上述
curl
请求生成以下go代码:

 // Generated by curl-to-Go: https://mholt.github.io/curl-to-go

body := strings.NewReader(`fulladdress=22280+S+209th+Way%2C+Queen+Creek%2C+AZ+85142`)
req, err := http.NewRequest("POST", "http://www.homefacts.com/hfreport.html", body)
if err != nil {
    // handle err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := http.DefaultClient.Do(req)
if err != nil {
    // handle err
}
defer resp.Body.Close()
问题是,
curl
命令和go代码之间的响应一直不同。
curl
命令返回此响应正文:

<html><head><meta http-equiv="refresh" content="0;url=http://www.homefacts.com/address/Arizona/Maricopa-County/Queen-Creek/85142/22280-S-209th-Way.html"/></head></html>
但是仍然没有乐趣,go代码的输出仍然不同于
curl
one


关于如何从go获得相同的
curl
响应,有什么想法吗

谢谢@u_mulder为我指出了正确的方向。似乎默认的go
http
客户端默认遵循重定向头,而
curl
不遵循重定向头

下面是更新后的代码,它在go和
curl
之间生成相同的结果:

body := strings.NewReader(`fulladdress=22280+S+209th+Way%2C+Queen+Creek%2C+AZ+85142`)
req, err := http.NewRequest("POST", "http://www.homefacts.com/hfreport.html", body)
if err != nil {
    // handle err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

client := &http.Client{
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
        return http.ErrUseLastResponse
    },
}

resp, err := client.Do(req)
if err != nil {
    // handle err
}
defer resp.Body.Close()

因为
go
遵循重定向和
curl
不这样做?@u\u mulder你说得对!将客户端更改为不使用re direct可解决此问题。如果你想将此作为答案提交,请随意,我会接受(此处使用的代码:stackoverflow.com/questions/23297520/how-can-I-make-the-go-http-client-not-follow-redirects-automatically),我想你可以回答你的问题)
body := strings.NewReader(`fulladdress=22280+S+209th+Way%2C+Queen+Creek%2C+AZ+85142`)
req, err := http.NewRequest("POST", "http://www.homefacts.com/hfreport.html", body)
if err != nil {
    // handle err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

client := &http.Client{
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
        return http.ErrUseLastResponse
    },
}

resp, err := client.Do(req)
if err != nil {
    // handle err
}
defer resp.Body.Close()