Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form content type for a json HTTP POST?

Tags:

Just wanted a clarification of the form content types:

  1. application/x-www-form-urlencoded: This is where you can send params encoded with the url.

  2. multipart/form-data: ??

I need to send a JSON in the post (so it would have the type: text/x-json, I guess). So the question is, is multipart/form-data suitable for this purpose / is application/x-www-form-urlencoded better?

Also, would it be possible to send some params encoded in the url, and some data in the json?

like image 505
rmk Avatar asked Nov 22 '10 19:11

rmk


People also ask

What is the Content-Type for JSON?

text/plain was typically used for JSON, but according to IANA, the official MIME type for JSON is application/json .

What should be the Content-Type for post request?

You can send only the following content types in a POST request to Media Server: application/x-www-form-urlencoded. multipart/form-data.

Is Content-Type required for POST?

No, it's not mandatory. Per the HTTP 1.1 specification: Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body.

How do I make a post request in JSON?

POST requests In Postman, change the method next to the URL to 'POST', and under the 'Body' tab choose the 'raw' radio button and then 'JSON (application/json)' from the drop down. You can now type in the JSON you want to send along with the POST request. If this is successful, you should see the new data in your 'db.


2 Answers

It looks like people answered the first part of your question (use application/json).

For the second part: It is perfectly legal to send query parameters in a HTTP POST Request.

Example:

POST /members?id=1234 HTTP/1.1 Host: www.example.com Content-Type: application/json  {"email":"[email protected]"} 

Query parameters are commonly used in a POST request to refer to an existing resource. The above example would update the email address of an existing member with the id of 1234.

like image 199
Herman J. Radtke III Avatar answered Oct 19 '22 23:10

Herman J. Radtke III


I have wondered the same thing. Basically it appears that the html spec has different content types for html and form data. Json only has a single content type.

According to the spec, a POST of json data should have the content-type:
application/json

Relevant portion of the HTML spec

6.7 Content types (MIME types)
...
Examples of content types include "text/html", "image/png", "image/gif", "video/mpeg", "text/css", and "audio/basic".

17.13.4 Form content types
...
application/x-www-form-urlencoded
This is the default content type. Forms submitted with this content type must be encoded as follows

Relevant portion of the JSON spec

  1. IANA Considerations
    The MIME media type for JSON text is application/json.
like image 31
user132447 Avatar answered Oct 19 '22 22:10

user132447