Handling both JSON and form values in POST request body with unknown values in Golang

35 Views Asked by At

I have a HTTP server and now want to handle POST requests with multiple content types and unknown values. Specifically I want to be able to get the values from the POST request body, both if it's a raw JSON (application/json) and also if it is sent from a form (application/x-www-form-urlencoded or even multipart/form-data). Also the values are not known in advance, so I can't use a predefined struct to save the data into.

Is there some way to handle both cases simultaneously?

I know that for form values, you use r.ParseForm() (or r.Form and r.PostForm) to get the data and for JSON you would read r.Body where r is the request object. So if I want to handle both form and raw JSON, do I have to check for the content type and then use the appropriate methods or is there some better way?

0

There are 0 best solutions below