英文:
Golang NewRequest passing POST parameter to the API for testing
问题
这是我的测试方法,它创建一个新的请求并传递POST参数。
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)
问题是POST参数
没有被URL的处理函数接收到。
你能帮我设置正确的请求吗?
谢谢。
英文:
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
得分: 1
你需要为你的请求正确设置content-type头。
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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论