title: Handling JSON Post Request in Go
body: <p>So I have the following, which seems incredibly hacky, and I've been thinking to myself that Go has better designed libraries than this, but I can't find an example of Go handling a POST request of JSON data. They are all form POSTs. </p>
  
  <p>Here is an example request: <code>curl -X POST -d "{\"test\": \"that\"}" http://localhost:8082/test</code></p>
  
  <p>And here is the code, with the logs embedded:</p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "log"
      "net/http"
  )
  
  type test_struct struct {
      Test string
  }
  
  func test(rw http.ResponseWriter, req *http.Request) {
      req.ParseForm()
      log.Println(req.Form)
      //LOG: map[{"test": "that"}:[]]
      var t test_struct
      for key, _ := range req.Form {
          log.Println(key)
          //LOG: {"test": "that"}
          err := json.Unmarshal([]byte(key), &amp;t)
          if err != nil {
              log.Println(err.Error())
          }
      }
      log.Println(t.Test)
      //LOG: that
  }
  
  func main() {
      http.HandleFunc("/test", test)
      log.Fatal(http.ListenAndServe(":8082", nil))
  }
  </code></pre>
  
  <p>There's got to be a better way, right? I'm just stumped in finding what the best practice could be. </p>
  
  <p>(Go is also known as Golang to the search engines, and mentioned here so others can find it.)</p>