Which JSON content type do I use?
For JSON text:
application/json
The MIME media type for JSON text is
application/json
. The default encoding is UTF-8. (Source: RFC 4627)
For JSONP (runnable JavaScript) with callback:
application/javascript
Here are some blog posts that were mentioned in the relevant comments:
- Why you shouldn't use
text/html
for JSON - Internet Explorer sometimes has issues with
application/json
- A rather complete list of Mimetypes and what to use them for
- The official mime type list at IANA from @gnrfan's answer below
Can't send a post request when the 'Content-Type' is set to 'application/json'
It turns out that CORS only allows some specific content types.
The only allowed values for the Content-Type header are:
- application/x-www-form-urlencoded
- multipart/form-data
- text/plain
Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
To set the content type to be 'application/json', I had to set a custom content type header in the API. Just removed the last header and added this one:
->header('Access-Control-Allow-Headers', 'Content-Type');
and it is working all good.
How to POST JSON data with Python Requests?
Starting with Requests version 2.4.2, you can use the json=
parameter (which takes a dictionary) instead of data=
(which takes a string) in the call:
>>> import requests
>>> r = requests.post('http://httpbin.org/post', json={"key": "value"})
>>> r.status_code
200
>>> r.json()
{'args': {},
'data': '{"key": "value"}',
'files': {},
'form': {},
'headers': {'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'close',
'Content-Length': '16',
'Content-Type': 'application/json',
'Host': 'httpbin.org',
'User-Agent': 'python-requests/2.4.3 CPython/3.4.0',
'X-Request-Id': 'xx-xx-xx'},
'json': {'key': 'value'},
'origin': 'x.x.x.x',
'url': 'http://httpbin.org/post'}
Content Type of POST request payload
I am using http.DetectContentType to return the content type o the request but it is returning text/plain is every scenario.
According to the documentation DetectContentType
"... implements the algorithm described at https://mimesniff.spec.whatwg.org/ to determine the Content-Type of the given data". The algorithm there is primarily for handling content types which the browser can handle by itself.
And if you look at the actual code you'll see that it does not care about application/json
or similar at all and that it returns text/plain
for anything which looks non-binary (and was not matched before as with text/html
).
In other words: this is the wrong tool for the job. The proper way would be for the client to specify what kind of content is sent using the Content-Type
header and not to let the server guess the kind of content.
HTTP Content-Type Header and JSON
The Content-Type
header is just used as info for your application. The browser doesn't care what it is. The browser just returns you the data from the AJAX call. If you want to parse it as JSON, you need to do that on your own.
The header is there so your app can detect what data was returned and how it should handle it. You need to look at the header, and if it's application/json
then parse it as JSON.
This is actually how jQuery works. If you don't tell it what to do with the result, it uses the Content-Type
to detect what to do with it.
Post request to include 'Content-Type' and JSON
Browsers do not support JSON as a media type for form submissions (the supported types are listed in the spec).
The only way to make such a request from a web page is to use the XMLHttpRequest object.
Google provide a JavaScript library (which wraps XMLHttpRequest) that can interact with their URL Shortener API.
Related Topics
Jquery Datepicker Won't Work on a Ajax Added HTML Element
How to Apply a CSS Style to an Element Name
What's The Difference Between Meta Name and Meta Property
CSS Right Margin Does Not Work Inside a Div with Overflow Scroll
Vertically Center Text in a 100% Height Div
Context.Getimagedata() on Localhost
Excel Crashes When Attempting to Inspect Dispstaticnodelist
HTML 5 Page Without Main <Header>
How to Change Style of Radio and Checkbox Input
Does HTML5 Change The Standard for HTML Commenting
How to Vertically Align Both Image and Text in a Div Using CSS
Post Values from a Multiple Select
Floated Child Elements: Overflow:Hidden or Clear:Both
Convert HTML Character Entity Encoding in R
How to Make Link Not Change Color After Visited