-1

I know that you can get the body content from

ioutil.ReadAll(c.Request.Body)

But using httputil.DumpRequest

dump, err := httputil.DumpRequest(c.Request, true)

will give the body contents along with other values, the body content in the end.

Content type: application/json IP: 127.0.0.1:36846 header token: Content length: 76 Request Method: POST Request URL: /signup Body: POST /signup HTTP/1.1 Host: 127.0.0.1:8080 Accept: / Accept-Encoding: gzip,deflate Accept-Language: en-US,en;q=0.8 Connection: keep-alive Content-Type: application/json Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36 {"fname":"aFirstName", "lname":"aLName", "email":"test@test.com", "password":"123"}

Is there an efficient way I can get only the body content from httputil.DumpRequest()? i.e In this case only

{"fname":"aFirstName", "lname":"aLName", "email":"test@test.com", "password":"123"}

Sanath Ballal
  • 1,648
  • 4
  • 22
  • 31
  • If the answer to your previous question is not correct don't duplicate the question but comment on the answer there and/or edit the question. – Dave C Aug 16 '15 at 12:24
  • 2
    This is [an X/Y question](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), you're asking how to do Y (muck with the output of a debugging function) when you should be doing X (duplicating an `io.ReadCloser`). The answer to this question is "you don't". – Dave C Aug 16 '15 at 12:52
  • 1
    @dave-c, I don't see this as a duplicate as the other question was how to "retain" the body. Here I am trying to ignore the garbage and extract only what I need. I am a beginner in Go. Any help is highly appreciated. Thanks. – Sanath Ballal Aug 16 '15 at 13:04
  • Very strictly speaking it isn't a duplicate; however this question only exists because of a poor answer to the other question (and close reasons are limited). It is not something that anyone should do. Don't use a debug function that isn't appropriate. – Dave C Aug 16 '15 at 13:46

1 Answers1

2

You don't use httputil.DumpRequest for that, that's a debug function.

Assuming you want to parse json you can do something like this:

defer c.Request.Body.Close()
var data yourDataType
if err := json.NewDecoder(c.Request.Body).Decode(&data); err != nil {
    // handle error
}
// handle data
OneOfOne
  • 95,033
  • 20
  • 184
  • 185