doushan6692 2018-02-18 11:14
浏览 480

GoLang HTTP GET-400错误请求

I am making HTTP GET call using below GoLang code:

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

Receiving error response:

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

To print above error response, I am using simple:

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

When I pick up getUrl and execute simply in Chrome browser, it returns success response:

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

Chrome request header:

    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 response headers:

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

Please help.

  • 写回答

1条回答 默认 最新

  • doz22551 2018-02-18 12:00
    关注

    Apologies, I found the answer. Earlier I use to create big getUrl by appending all parameters to it. After doing below, the problem was resolved.

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

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条