score:338

Accepted answer

in order to access the data from a readablestream you need to call one of the conversion methods (docs available here).

as an example:

fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then(function(response) {
    // the response is a response instance.
    // you parse the data into a useable format using `.json()`
    return response.json();
  }).then(function(data) {
    // `data` is the parsed version of the json returned from the above endpoint.
    console.log(data);  // { "userid": 1, "id": 1, "title": "...", "body": "..." }
  });

edit: if your data return type is not json or you don't want json then use text()

as an example:

fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then(function(response) {
    return response.text();
  }).then(function(data) {
    console.log(data); // this will be a string
  });

hope this helps clear things up.

score:-1

i just had the same problem for over 12 hours before reading next, just in case this helps anyone. when using nextjs inside your _api page you will need to use json.stringify(whole-response) and then send it back to your page using res.send(json.stringify(whole-response)) and when it's received on the client side you need to translate it back into json format so that it's usable. this can be kinda figured out by reading their serialization section. hope it helps.

score:4

i dislike the chaining thens. the second then does not have access to status. as stated before 'response.json()' returns a promise. returning the then result of 'response.json()' in a acts similar to a second then. it has the added bonus of being in scope of the response.

return fetch(url, params).then(response => {
    return response.json().then(body => {
        if (response.status === 200) {
            return body
        } else {
            throw body
        }
    })
})

score:11

if you just want the response as text and don't want to convert it into json, use https://developer.mozilla.org/en-us/docs/web/api/body/text and then then it to get the actual result of the promise:

fetch('city-market.md')
  .then(function(response) {
    response.text().then((s) => console.log(s));
  });

or

fetch('city-market.md')
  .then(function(response) {
    return response.text();
  })
  .then(function(mytext) {
    console.log(mytext);
  });

score:13

note that you can only read a stream once, so in some cases, you may need to clone the response in order to repeatedly read it:

fetch('example.json')
  .then(res=>res.clone().json())
  .then( json => console.log(json))

fetch('url_that_returns_text')
  .then(res=>res.clone().text())
  .then( text => console.log(text))

score:19

little bit late to the party but had some problems with getting something useful out from a readablestream produced from a odata $batch request using the sharepoint framework.

had similar issues as op, but the solution in my case was to use a different conversion method than .json(). in my case .text() worked like a charm. some fiddling was however necessary to get some useful json from the textfile.

score:37

res.json() returns a promise. try ...

res.json().then(body => console.log(body));

score:79

some people may find an async example useful:

var response = await fetch("https://httpbin.org/ip");
var body = await response.json(); // .json() is asynchronous and therefore must be awaited

json() converts the response's body from a readablestream to a json object.

the await statements must be wrapped in an async function, however you can run await statements directly in the console of chrome (as of version 62).


Related Query

More Query from same tag