title: How to get the JSON from the Body of a Request on Go
body: <p>I'm a newbie with Go, but so far I'm liking it very much.</p>
  
  <p>I have a problem I can't figure out. I'm migrating an API from Node to Go and there is this log where I have to capture the Body of a POST <strong><em>AS IT IS</em></strong> and save it to a <code>jsonb</code> type column in a Postgresql database. </p>
  
  <p>This means I can't use a <code>struct</code> or anything predetermined.</p>
  
  <p>The POST is made with body raw <code>Content-Type: application/json</code> like so:</p>
  
  <pre><code>{
      "debug": false,
      "order_id_gea": 326064,
      "increment_id_gea": 200436102,
      "date": "2017-05-18T01:44:44+00:00",
      "total_amount": 10000.00,
      "currency": "MXN",
      "payment_method": "Referencia bancaria",
      "reference": "857374",
      "buyer": {
          "buyer_id_gea": 1234,
          "full_name": "Juan Perez Martinez",
          "email": "juanperez@gmail.com",
          "phone": "5512341234"
      },
      "products": [
          {
              "sku":"PEP16114",
              "price": 10000.00,
              "currency": "MXN",
              "student": {
                  "school_id_gea": 172,
                  "grade_id_gea": 119,
                  "level_id_gea": 36,
                  "name": "Benancio",
                  "last_name": "Perez",
                  "second_last_name": "Garcia",
                  "email": "benancio@gmail.com"
              }
          }
      ]
  }
  </code></pre>
  
  <p>On Node + Hapi is quite simple:</p>
  
  <pre><code>const payload = request.payload
  </code></pre>
  
  <p>and then I can access the JSON from <code>payload</code>.</p>
  
  <p>I am using Go with Echo, so <a href="https://godoc.org/github.com/labstack/echo#Context" rel="noreferrer"><code>context</code></a> is a wrapper where I can find <code>Request() *http.Request</code>.</p>
  
  <p>I have tried the following, but everytime the result is empty or an error because it is empty:</p>
  
  <pre><code>var v interface{}
  err := json.NewDecoder(context.Request().Body).Decode(&amp;v)
  if err != nil {
      return result, err
  }
  fmt.Println(v)
  </code></pre>
  
  <p>Result: EOF</p>
  
  <p>--</p>
  
  <pre><code>m := echo.Map{}
  if err := context.Bind(&amp;m); err != nil {
      return result, err
  }
  fmt.Println(m)
  </code></pre>
  
  <p>Result code 400, message EOF </p>
  
  <p>--</p>
  
  <pre><code>body, error := ioutil.ReadAll(context.Request().Body)
  if error != nil {
      return result, error
  }
  fmt.Println(body)
  </code></pre>
  
  <p>Result []</p>
  
  <p>--</p>
  
  <p>What am I missing and/or doing wrong? Thanks!</p>