Posting JSON data problem with content type
See original GitHub issueI used the example provided on the readme file.
fetch('/users', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'Hubot',
login: 'hubot',
})
})
I got the following headers on devtools. The problem is marked with a “//” comment. content type is really not allowing us to set it to application/json, I have tried a 100 different ways.
As result I set up a simple node js server to see request.body (always is empty)
If I use curl http://localhost:3001/log -H 'Content-Type: application/json' -d '{"longUrl": "http://www.google.com/"}'
this works fine. I can see the request.body
General
===
Request URL:http://localhost:3001/log
Request Method:POST
Status Code:200 OK
Remote Address:[::1]:3001
Response Headers
===
Connection:keep-alive
Content-Length:16
Content-Type:application/json; charset=utf-8
Date:Mon, 18 Apr 2016 10:04:14 GMT
ETag:W/"10-c2PoX+nt7m8FOksxlYjAhg"
X-Powered-By:Express
Request Headers
===
accept:application/json
Accept-Encoding:gzip, deflate
Accept-Language:en-GB,en;q=0.8,sq;q=0.6,it;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Content-Length:32
content-type:text/plain;charset=UTF-8 //the problem//
Host:localhost:3001
Origin:http://xxxxxxxxxxxxxxx.co.uk
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36
Request Payload
===
{name: "Hubot", login: "hubot"}
login
:
"hubot"
name
:
"Hubot"
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (3 by maintainers)
Top Results From Across the Web
The application/problem+json Content-Type - SymfonyCasts
So the json part tells us how to parse the document. The problem part give us a hint on how to find out...
Read more >error : use 'application/json' Content-Type and raw POST with ...
According to this answer, the problem seems to be with the request missing the relevant Content-Type header set to application\json . Please add ......
Read more >Using POST method with application/json doesn't send message
I have a simple application that I want to use http to POST a message to an external endpoint with a json string...
Read more >Request body not a json exception from REST DIspatcher in ...
the Content type in Request header is 'application/json',I get Request body not a json format error. We do not have any requirement to...
Read more >Handling API errors with Problem JSON - Celonis Engineering
A Problem JSON payload should have at least one property: the "type" field (➀). "type" is a unique identifier for the error at...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@bo3en, the problem is that when you work in ‘mode’ ‘no-cors’, the Headers become an immutable and you will not be able to change some of its entries. One of the heads you can’t change is the Content-Type. When you set ‘mode’ to ‘no-cors’ you will be able to change only these headers:
Accept
Accept-Language
Content-Language
Content-Type
and whose value, once parsed, has a MIME type (ignoring parameters) that isapplication/x-www-form-urlencoded
,multipart/form-data
, ortext/plain
In another words, in ‘mode’ '-no-‘cors’ you can only set
application/x-www-form-urlencoded
,multipart/form-data
, ortext/plain
to theContent-Type
.So the solution is stop using fetch or changing it to ‘cors’ ‘mode’. Of course this will only works if your server also accepts ‘cors’ requests.
Here is an example of how you could enable CORS in an Apache Server
The above code will inject the CORS headers in the response when its necessary. With this code your server will allow CORS only from the the domains “url1.com” or “url2.com”.
Here are some references
I am still getting issue on using fetch call. It is changing “Content-Type” from ‘application/json’ to ‘text/plain;charset=UTF-8’ while i run this code. If someone faced same kind of issue.