0

I am trying to make a POST request using Javascript's fetch method as described here.

I get a ReadableStream instead of a usual json response, such as what I would get with jQuery, Angular, whatever.

My code is here: https://jsbin.com/vuwilaviva/edit?css,js,output

var request = new Request('https://httpbin.org/post', {
    method: 'POST', 
    mode: 'cors',
    data: 'The sky is green',
    redirect: 'follow',
    headers: new Headers({
        'Content-Type': 'text/plain'
    })
});


// Now use it!
fetch(request).then(function(resp) {
  console.log('Logging response...')
  console.log(resp);
});

The test API endpoint works fine with postman, curl, etc, so I am sure I am using fetch wrong, and it's not an issue with the API (it just returns whatever string is passed to it as data):

enter image description here

Edit: The current answer doesn't actually get the data returned by the post request - it's nowhere to be found in the logged json:

enter image description here

7
  • You pass data in the body property. Commented Jul 25, 2017 at 20:54
  • That link tells you how to get JSON from the response, in the section with the header Handling JSON... Commented Jul 25, 2017 at 20:59
  • @MikeMcCaughan Doesn't work. Commented Jul 26, 2017 at 14:57
  • Can you try something else that it details on that page? You seem to be a bit confused as to how exactly JSON is used. Your last edit shows what was returned in the Postman result above it, as a JavaScript object, which is what JSON is meant to provide. If you don't want JSON, but just want text, see the section with the header Handling Basic Text/HTML Responses just below the section I pointed to you... Commented Jul 26, 2017 at 15:38
  • @MikeMcCaughan Changing response type to text just gets me the json object as text. I still don't see the data I need. And yes, I did try what you pointed me to. Commented Jul 26, 2017 at 15:51

1 Answer 1

3

response.json should be used for this

fetch(request)
.then(response => response.json())
.then(json => console.log(json));
Sign up to request clarification or add additional context in comments.

9 Comments

fun fact: you can now use .then(console.log) in chrome, maybe others (they fixed this)
@pethel: Two things here - 1. This doesn't get me what I need - I don't see the response string in the json. 2. Can you explain your answer, why the two then statements? Isn't the call already async, why is the response a stream?
@VSO your issue is the contenttype response.json() -> response.text() now and you can log the content as text. Try to change to application/json but you also need to adjust your server code because.
@pethel My "backend" is the test API here httpbin.org. It works just fine using anything by fetch, so I don't imagine that's the problem, unless I don't understand some implementation detail.
@VSO jsbin.com/cedaxabida/edit?css,js,output , hope you see the update. I changed the headers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.