Sometimes, we want to call a REST web service API from JavaScript.
In trhis article, we’ll look at how to call a REST web service API from JavaScript.
How to call a REST web service API from JavaScript?
To call a REST web service API from JavaScript, we use fetch
.
For instance, we write
const userAction = async () => {
const response = await fetch("http://example.com/movies.json");
const myJson = await response.json();
// ...
};
to define the userAction
function.
In it, we call fetch
with a URL to make a get request to it.
Then we get the JSON response body with response.json
.
Conclusion
To call a REST web service API from JavaScript, we use fetch
.