Created
July 16, 2020 19:49
-
-
Save micahlt/403c16249e1517aaf7b3bf5baec8e8e3 to your computer and use it in GitHub Desktop.
An example of a simple ES6 fetch request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fetch('http://example.com/api-endpoint', { | |
| method: 'POST', // this can be GET, POST, PUT, DELETE, etc depending on the API's standards | |
| body: 'hello, api' // this can be any data you choose to transmit through a POST or PUT request | |
| // there are other options as well, check out the MDN article | |
| }) | |
| .then(response => response.json()) // the response object has several properties | |
| // the most commonly used are .json() and .status() | |
| .then(data => { | |
| console.log(data); // now you can do whatever you want with the data, which is the property of the | |
| // response object you passed above. You can also pass the entire response object | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment