Send JSON request to test Endpoint API in beego is failing with empty body
<p>I am trying to test the endpoint of my REST API's using beego framework.</p>

<p>My test function is below that I am using to send JSON request:</p>

<pre><code>func testHTTPJsonResp(url string) string {
    var jsonStr = []byte(`{"title":"Buy cheese and bread for breakfast."}`)
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.Header.Set("X-Custom-Header", "myvalue")
    req.Header.Set("Content-Type", "application/json")

    beego.Error(err)
    w := httptest.NewRecorder()
    beego.BeeApp.Handlers.ServeHTTP(w, req)

    beego.Debug(w)

    return w.Body.String()
}
</code></pre>

<p>The server does receive the request but the input body is always <code>empty</code> for the request.</p>

<p>Similar, function that I am using to send Form data to server <code>works fine</code>.</p>

<pre><code>func testHTTPResp(httpProt, url string, params map[string]interface{}) string {
    bodyBuf := &amp;bytes.Buffer{}
    bodyWriter := multipart.NewWriter(bodyBuf)

    for key, val := range params {
        beego.Error(key + val.(string))
        _ = bodyWriter.WriteField(key, val.(string))

    }

    contentType := bodyWriter.FormDataContentType()
    bodyWriter.Close()

    r, _ := http.NewRequest(httpProt, url, bodyBuf)
    r.Header.Add("Content-Type", contentType)

    w := httptest.NewRecorder()
    beego.BeeApp.Handlers.ServeHTTP(w, r)

    beego.Debug(w)

    return w.Body.String()
}
</code></pre>

<p><strong>Issue:</strong> Why is the server receiving JSON request body as empty while similar form-encoded data goes fine. Have been stuck on this for a few days now, any pointers are highly appreciated.</p>