Complete the code to fetch data from an external API using fetch.
const response = await fetch([1]);
const data = await response.json();The fetch function requires the full URL as a string to call the external API.
Complete the code to set the HTTP method to POST in the fetch options.
const response = await fetch('https://api.example.com/data', { method: [1] });
To send data to the API, the HTTP method must be set to 'POST'.
Fix the error in setting the request headers to send JSON data.
const response = await fetch('https://api.example.com/data', { method: 'POST', headers: [1], body: JSON.stringify({ key: 'value' }) });
Headers must be an object with string keys and string values. The key 'Content-Type' must be quoted and the value must be a string.
Fill both blanks to correctly handle the fetch response and parse JSON.
const response = await fetch('https://api.example.com/data'); const [1] = await response.[2]();
The response object has a method json() to parse the response body as JSON. We store the result in a variable, commonly named 'data'.
Fill all three blanks to send a POST request with JSON body and handle the response.
const response = await fetch([1], { method: [2], headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: 'Hello' }) }); const result = await response.[3]();
To send data, use the POST method and the correct URL. Then parse the response with response.json().