GoLang HTTP GET - 400错误请求

时间:2018-02-18 11:14:13

标签: http go get bad-request

我使用GoLang代码进行HTTP GET调用:

client := &http.Client{}
req, _ := http.NewRequest("GET", getUrl, nil)
//req.Header.Set("Accept-Encoding", "br")
response, _ := client.Do(req)

收到错误回复:

<html>
     <head><title>400 Bad Request</title></head>
     <body bgcolor="white">
          <center><h1>400 Bad Request</h1></center>
          <hr><center>nginx</center>
     </body>
</html>

要打印上面的错误响应,我使用简单:

data, _ := ioutil.ReadAll(response.Body)
log.Println(string(data))

当我拿起getUrl并在Chrome浏览器中执行时,它会返回成功响应:

{"message":"SUCCESS","errorcode": "0" ,"errormessage":""}

Chrome请求标头:

    Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    Accept-Encoding:gzip, deflate, br
    Accept-Language:en-GB,en-US;q=0.9,en;q=0.8
    Cache-Control:max-age=0
    Connection:keep-alive
    Host:api.pepipost.com
    Upgrade-Insecure-Requests:1
    User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36

Chrome回复标题:

Access-Control-Allow-Headers:accept, content-type
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Type:application/json
Date:Sun, 18 Feb 2018 11:01:28 GMT
Server:nginx
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Firefox-Spdy:3.1
X-Frame-Options:SAMEORIGIN
X-Server1:Region-US
X-XSS-Protection:1; mode=block

请帮忙。

1 个答案:

答案 0 :(得分:0)

道歉,我找到了答案。之前我通过将所有参数附加到它来创建大型getUrl。完成下面的操作后,问题就解决了。

q := req.URL.Query()
q.Add("key", "value")
req.URL.RawQuery = q.Encode()