POST data faild using http.NewRequest

前端 未结 1 415
轻奢々
轻奢々 2021-01-24 08:08

I am trying to pass data from one golang service to another using http.NewRequest(). To do it I used following code:

        httpClient := http.Clie         


        
1条回答
  •  伪装坚强ぢ
    2021-01-24 08:36

    From the docs for ParseForm:

    [...] when the Content-Type is not application/x-www-form-urlencoded, the request Body is not read, and r.PostForm is initialized to a non-nil, empty value.

    PostForm sets the Content-Type automatically, but now you have to do it yourself:

    req, err := http.NewRequest("POST", userserviceUrl, strings.NewReader(form.Encode()))
    // TODO: handle error
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    

    0 讨论(0)
提交回复
热议问题