question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Posting JSON data problem with content type

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

23reactions
stavarengocommented, Sep 26, 2016

@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 is application/x-www-form-urlencoded, multipart/form-data, or text/plain

In another words, in ‘mode’ '-no-‘cors’ you can only set application/x-www-form-urlencoded, multipart/form-data, or text/plain to the Content-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

SetEnvIfNoCase Access-Control-Request-Method "(GET|POST|PUT|DELETE|OPTIONS)" IsPreflight=1
SetEnvIfNoCase Origin ".*" AccessControlAllowOrigin=$0
SetEnvIfNoCase Origin "https://(url1.com|url2.com)$" AccessControlAllowOrigin=$0

Header always set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" env=IsPreflight
Header always set Access-Control-Allow-Headers "Content-Type, Authorization, Accept, Accept-Language" env=IsPreflight
Header always set Access-Control-Max-Age "7200" env=IsPreflight
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteCond %{ENV:IsPreflight} 1
RewriteRule ^(.*)$ $1 [R=204,L]

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

0reactions
AlpeshMNcommented, Jul 13, 2018

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.

screen shot 2018-07-13 at 3 03 16 pm screen shot 2018-07-13 at 3 04 23 pm
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found