Form content type for a json HTTP POST?

2019-03-22 19:46发布

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?

标签: json http forms
3条回答
男人必须洒脱
2楼-- · 2019-03-22 20:10
multipart/form-data

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

查看更多
老娘就宠你
3楼-- · 2019-03-22 20:21

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.
查看更多
看我几分像从前
4楼-- · 2019-03-22 20:34

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":"user@example.com"}

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.

查看更多
登录 后发表回答