doushi1473 2018-10-20 14:18
浏览 43

如何传递原始数据以发布请求

I have faced some weird issue, I am sending request with Postman (using raw body) and getting correct response, but When I am making request with go, I am getting different response.

I tried this:

url := "http://example.com"
payload := strings.NewReader("key=value")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("content-type", "application/x-www-form-urlencoded")
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")
req.Header.Add("cache-control", "no-cache")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)

fmt.Println(res)
fmt.Println(string(body))

But got wrong response, also tried using values

values := url.Values{}
values.Add("key", "value")
payload := strings.NewReader(values.Encode())

But still getting wrong response, is there a way to simulate postman's raw method in Go?

Postman is automatically adding cookies to my request, just added Cookie header and the problem is fixed

  • 写回答

1条回答 默认 最新

  • dongshao8471 2018-10-22 13:20
    关注

    Here is a postman tip: click on the code button/link right below the send button, next to the cookies link and from the popup you can select which language to get code output of the what postman would execute, Go is an option here

    For your example I think what you need to do is remove the line that explicitly sets the content-type to have it match the raw body response in postman

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP