Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON vs Form POST

We're having a bit of a discussion on the subject of posting data to a REST endpoint. Since the objects are quite complex, the easiest solution is to simply serialize them as JSON and send this in the request body.

Now the question is this: Is this kosher? Or should the JSON be set as a form parameter like data=[JSON]? Or is sending of JSON in the request body just frowned upon for forcing the clients using the application, to send their data via JavaScript instead of letting the browser package it up as application/x-www-form-urlencoded?

I know all three options work. But which are OK? Or at least recommended?

like image 484
Johan Öbrink Avatar asked Dec 22 '11 13:12

Johan Öbrink


People also ask

Which is better form data or JSON?

JSON seems to have a lot of benefits over formData for sending data to server. Some of them include sending nested data without having to manually stringfy, or making possible a simple code like below to work: data() { return { form: {}, } }, methods: { submit() { this.

How is form data different from JSON?

The main benefit of json over formdata is fields nesting! With json, you can nest fields as you want(dunno if there is a limit), but with formdata, you have to manually stringify the fields first and then add them as a string to the key that would own that nested object.

Can I send form data as JSON?

Using the JSON. stringify() method then format the plain form data as JSON. Specify the HTTP request method as POST and using the header field of the Fetch API specify that you are sending a JSON body request and accepting JSON responses back. Then set the request body as JSON created from the form fields.

Does Post use JSON?

To post JSON data to the server, we need to use the HTTP POST request method and set the correct MIME type for the body. The correct MIME type for JSON is application/json. In this POST JSON example, the Content-Type: application/json request header specifies the media type for the resource in the body.


1 Answers

I'd say that both methods will work well it's important that you stay consistent across your APIs. The option I would personally choose is simply sending the content as application/json.

POST doesn't force you to use application/x-www-form-urlencoded - it's simply something that's used a lot because it's what webbrowsers use.

like image 58
Tom van der Woerdt Avatar answered Sep 22 '22 00:09

Tom van der Woerdt