Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to make a GET request to read data from the server.
Rest API
fetch('[1]') .then(response => response.json()) .then(data => console.log(data));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Including HTTP method in the URL string
Using wrong HTTP methods like POST or DELETE
✗ Incorrect
The GET request URL should be the endpoint string only, like '/api/data'.
2fill in blank
mediumComplete the code to specify the GET method explicitly in the fetch options.
Rest API
fetch('/api/items', { method: '[1]' }) .then(response => response.json()) .then(data => console.log(data));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET
Omitting the method field when needed
✗ Incorrect
To read data, the HTTP method should be 'GET'.
3fill in blank
hardFix the error in the fetch call to correctly read JSON data from the response.
Rest API
fetch('/api/users') .then(response => response.[1]()) .then(data => console.log(data));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using response.text() instead of response.json()
Trying to parse JSON with blob() or formData()
✗ Incorrect
Use response.json() to parse the response body as JSON.
4fill in blank
hardFill both blanks to create a GET request with headers to accept JSON.
Rest API
fetch('/api/products', { method: '[1]', headers: { 'Accept': '[2]' } }) .then(response => response.json()) .then(data => console.log(data));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST method instead of GET
Setting Accept header to 'text/html' instead of 'application/json'
✗ Incorrect
The method should be 'GET' and the Accept header should be 'application/json' to request JSON data.
5fill in blank
hardFill all three blanks to fetch a resource by ID and handle errors properly.
Rest API
fetch(`/api/items/[1]`) .then(response => { if (!response.[2]) throw new Error('Network error'); return response.json(); }) .then(data => console.log(data)) .catch(error => console.error('[3]', error));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking response.status instead of response.ok
Not including the resource ID in the URL
Not handling errors with catch
✗ Incorrect
Use the resource ID '42' in the URL, check response.ok for success, and log 'Fetch failed' on error.