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.

json body not posted

See original GitHub issue

Hi. I’m trying to make a post request with json in the body included. The request is indeed a post request but the json data isn’t posted.

Here is an example:

fetch("http://127.0.0.1:8080/v1.0/users", {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        email: 'foo',
        pass: 'bar'
      })
    }).then(response => console.log(response))

Results in the following request headers:

POST /v1.0/users HTTP/1.1 Host: 127.0.0.1:8080 Connection: keep-alive Content-Length: 28 accept: application/json Origin: http://127.0.0.1:3000 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36 content-type: application/json DNT: 1 Referer: http://127.0.0.1:3000/registration Accept-Encoding: gzip, deflate Accept-Language: nl,en-US;q=0.8,en;q=0.6

There is no data posted. Can anyone point me in the right direction? Thanks!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:20
  • Comments:48 (3 by maintainers)

github_iconTop GitHub Comments

73reactions
dwiltcommented, Jul 10, 2016

@npassaro ok, I got it - the following works. Followed the example on MDN:

var myHeaders = new Headers();

myHeaders.append('Content-Type', 'application/json');

fetch('/contact-form', {
    method: 'POST',
    headers: myHeaders,
    mode: 'cors',
    cache: 'default',
    body: JSON.stringify(fields)
}).then(() => {
    dispatch(contactFormSubmitSuccess());
});
55reactions
npassarocommented, Apr 27, 2016

I have the exact same problem, did anyone know a fix for this?

From what I’ve observed, the problem seems to come from the Content-Type header not being set.

const request = new Request(`${config.apiUrl}/jobs`, {
    method: 'POST',
    mode: 'no-cors',
    redirect: 'follow',
    body: JSON.stringify({ job }),
    headers: new Headers({
      'Content-Type': 'application/json',
      Accept: 'application/json',
    })
  });
console.log(request.headers.getAll('Content-Type'));

On the console I only only see: ["text/plain;charset=UTF-8"]

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why simple json request body in Postman is not working
The POST URL you are using might be wrongly written. 400 typically means you passed something invalid to the API.
Read more >
JSON POST request using app sends empty body · Issue #8455
Endpoint receives request with right Content-Length and -Type but the Body is empty. Changing to Raw Text in Postman with the same bod......
Read more >
Making a JSON POST Request With HttpURLConnection
A quick and practical introduction to issuing POST requests using HttpURLConnection.
Read more >
Add a Request Body to a POST Request | API Connector
If your POST request isn't working, a likely cause is a mismatched content type. Try adding in a content type into the header...
Read more >
How to receive JSON POST with PHP ? - GeeksforGeeks
In this article, we will see how to retrieve the JSON POST with PHP, ... stream that allows us to read raw data...
Read more >

github_iconTop Related Medium Post

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