1

I am getting one POST request in my server whose payload is something like that

{       "amount": 10000,
        "amount_due": 0,
        "amount_paid": 10000,
        "attempts": 1,
}

and content-type is application/json. Now for some calculation I want that payload in raw text something like that.

{"amount":10000,"amount_due":0,"amount_paid":10000,"attempts":1} 
No space and no new line

I am using Golang and gin framework, but which I am trying to get the request body like ginCtx *gin.Context.Request.Body or ginCtx *gin.Context.GetRawData(), then I am not getting my actual desired raw data, I am getting well indented json, but I want raw body. Please help me how to get that in Golang using gin framework.

1 Answers1

2

but I want raw body

Note that raw means unprocessed, which is exactly what c.GetRawData() returns.

If you want to take the raw data and remove all insignificant whitespace then you need to process the data. The result of that processing, by definition, will not be raw data anymore.

So it is not very clear what you are asking for.

raw, err := c.GetRawData()
if err != nil {
    return err
}
var buf bytes.Buffer
if err := json.Compact(&buf, raw); err != nil {
    return err
}
data := buf.Bytes()
fmt.Println(string(data))
mkopriva
  • 35,176
  • 4
  • 57
  • 71