rmk
rmk

Reputation: 4455

Form content type for a json HTTP POST?

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?

Upvotes: 27

Views: 120975

Answers (3)

Herman J. Radtke III
Herman J. Radtke III

Reputation: 1812

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.

Upvotes: 31

user132447
user132447

Reputation: 1701

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.

Upvotes: 6

Lorenzo
Lorenzo

Reputation: 29427

multipart/form-data

is used when you want to upload files to the server. Please check this article for details.

Upvotes: 3

Related Questions