Complete the code to make a GET request to the API endpoint.
fetch('[1]') .then(response => response.json()) .then(data => console.log(data));
The correct URL for the GET request is 'https://api.example.com/data'.
Complete the code to send a POST request with JSON data.
fetch('https://api.example.com/data', { method: '[1]', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Alice' }) });
To send data, the HTTP method must be 'POST'.
Fix the error in the code to correctly parse the JSON response.
fetch('https://api.example.com/data') .then(response => response.[1]()) .then(data => console.log(data));
The response must be parsed with json() to get the JSON data.
Fill both blanks to create a request that sends JSON and checks for success.
fetch('https://api.example.com/data', { method: '[1]', headers: { 'Content-Type': '[2]' }, body: JSON.stringify({ age: 30 }) }) .then(response => { if (response.ok) { console.log('Success'); } });
The method must be 'POST' to send data, and the content type must be 'application/json' to indicate JSON data.
Fill all three blanks to create a request that sends data, parses JSON response, and handles errors.
fetch('https://api.example.com/data', { method: '[1]', headers: { 'Content-Type': '[2]' }, body: JSON.stringify({ city: 'Paris' }) }) .then(response => { if (!response.ok) throw new Error('Network error'); return response.[3](); }) .then(data => console.log(data)) .catch(error => console.error(error));
Use 'POST' to send data, 'application/json' as content type, and parse response with 'json()'.
