Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Text From Fetch Response Object

I'm using fetch to make API calls and everything works but in this particular instance I'm running into an issue because the API simply returns a string -- not an object.

Typically, the API returns an object and I can parse the JSON object and get what I want but in this case, I'm having trouble finding the text I'm getting from the API in the response object.

Here's what the response object looks like. enter image description here

I thought I'd find the text inside the body but I can't seem to find it. Where do I look?

like image 902
Sam Avatar asked Jan 30 '17 22:01

Sam


People also ask

How do I get fetch response text?

The Fetch API allows you to asynchronously request for a resource. Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.

How do I get json from fetch response?

To get JSON from the server using the Fetch API, you can use the response. json() method. The response. json() method reads the data returned by the server and returns a promise that resolves with a JSON object.

How do you get HTML response in Fetch?

The trick is to use the text() method instead of the json() method on the stream. This will return a text string of the HTML. fetch('/about'). then(function (response) { // The API call was successful!


1 Answers

Using the fetch JavaScript API you can try:

response.text().then(function (text) {   // do something with the text response  }); 

Also take a look at the docs on fetch > response > body interface methods

like image 115
Zach Tuttle Avatar answered Oct 18 '22 04:10

Zach Tuttle