douren0558
douren0558
2016-01-18 07:41
浏览 567
已采纳

Golang NewRequest将POST参数传递给API进行测试

This is my test method which creates a new request and passes POST param.

url1 := "/api/addprospect"
data := url.Values{}
data.Add("customer_name", "value")
b := bytes.NewBuffer([]byte(data.Encode()))
request, err := http.NewRequest("POST", serverHttp.URL+url1, b)
res, err := http.DefaultClient.Do(request)

The problem is the POST param is not getting picked up by the function handler of the url.

Can you please help me with setting up right request?

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqintong8972
    dongqintong8972 2016-01-18 07:58
    已采纳

    You need to properly set the content-type header for your request.

    request, err := http.NewRequest("POST", serverHttp.URL+url1, b)
    
    request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    
    res, err := http.DefaultClient.Do(request)
    
    点赞 评论

相关推荐