来自cURL和Golang POST的不同响应-无法理解原因 [英] Different Response From cURL and Golang POST - Can't Understand Why
问题描述
我正在尝试使用golang的 http
客户端从服务器获得响应.
I'm trying to get a response from a server using golang's http
client.
我要通过go执行的请求应与以下 curl
命令相同:
The request I'm looking to perform via go should be identical to the following curl
command:
curl --data "fulladdress=22280+S+209th+Way%2C+Queen+Creek%2C+AZ+85142" 'http://www.homefacts.com/hfreport.html'
我已经编写了等效的go代码,还尝试使用名为 curl的优质服务-to-go ,它为上述 curl
请求生成以下go代码:
I've coded the equivalent go code, and also tried using a nice service called curl-to-go, which generates the following go code for the above curl
request:
// 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
命令返回以下响应正文:
The problem is that I keep getting a different response between the curl
command and the go code. The curl
command returns this response body:
<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代码返回的长度为 HTML
,这不是预期的结果.
which is the expected result. However the go code returns a lengthly HTML
which is not the expected result.
我尝试将-verbose
添加到 curl
命令以复制其所有标头,因此我通过go代码添加了以下标头:
I've tried adding --verbose
to the curl
command to copy all it's headers, so I added the following headers via my go code:
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "curl/7.51.0")
req.Header.Set("Accept", "*/*")
req.Header.Set("Content-Length", "56")
但是仍然没有喜悦,go代码的输出与 curl
的输出仍然不同.
But still no joy, the output from the go code remains different than the curl
one.
关于如何从go中获得相同的 curl
响应的任何想法?
Any ideas on how to get the same curl
response from go?
推荐答案
感谢@u_mulder向我指出正确的方向.似乎默认情况下默认的go http
客户端遵循重定向标头,而 curl
则不这样做.
Thanks @u_mulder for pointing me out in the right direction. It seems that the default go http
client follows the redirect header by default, while curl
does not.
以下是更新的代码,可在go和 curl
之间生成相同的结果:
Here is the updated code that generates the same results between go and 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()
这篇关于来自cURL和Golang POST的不同响应-无法理解原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!