title: Make a URL-encoded POST request using `http.NewRequest(...)`
body: <p>I want to make a POST request to an API sending my data as a <code>application/x-www-form-urlencoded</code> content type. Due to the fact that I need to manage the request headers, I'm using the <a href="http://golang.org/pkg/net/http/#NewRequest"><code>http.NewRequest(method, urlStr string, body io.Reader)</code></a> method to create a request. For this POST request I append my data query to the URL and leave the body empty, something like this:</p>
  
  <pre><code>package main
  
  import (
      "bytes"
      "fmt"
      "net/http"
      "net/url"
      "strconv"
  )
  
  func main() {
      apiUrl := "https://api.com"
      resource := "/user/"
      data := url.Values{}
      data.Set("name", "foo")
      data.Add("surname", "bar")
  
      u, _ := url.ParseRequestURI(apiUrl)
      u.Path = resource
      u.RawQuery = data.Encode()
      urlStr := fmt.Sprintf("%v", u) // "https://api.com/user/?name=foo&amp;surname=bar"
  
      client := &amp;http.Client{}
      r, _ := http.NewRequest("POST", urlStr, nil)
      r.Header.Add("Authorization", "auth_token=\"XXXXXXX\"")
      r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
      r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
  
      resp, _ := client.Do(r)
      fmt.Println(resp.Status)
  }
  </code></pre>
  
  <p>As I response, I get always a <code>400 BAD REQUEST</code>. I believe the problem relies on my request and the API does not understand which payload I am posting. I'm aware of methods like <a href="http://golang.org/pkg/net/http/#Request.ParseForm"><code>Request.ParseForm</code></a>, not really sure how to use it in this context though. Maybe am I missing some further Header, maybe is there a better way to send payload as a <code>application/json</code> type using the <code>body</code> parameter?</p>