English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app

How to Read Response Body HTML with Javascript Fetch Method with React Native


In React Native you see a lot of tutorials and articles using the fetch method when interacting with JSON APIs. It's great for this and the the pattern looks like this

fetch(url).then((resp)=>{ return resp.json() }).then((json)=>{ console.log(json) })

But what if the API you're working with is returning HTML? The solution is pretty simple but kind of hard to find online because it's not as common as working with JSON.

Instead of returning resp.json() just return resp.text() like this

fetch(url).then((resp)=>{ return resp.text() }).then((text)=>{ console.log(text) })

This will give you the response body in plain text.

Tagged w/ #javascript #mobile #react-native #cors #response #apis #json