Complete the code to send a POST request using fetch.
fetch('https://example.com/api/data', { method: '[1]', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'John' }) })
The method property must be set to POST to send data to the server.
Complete the code to parse the JSON response from the fetch POST request.
fetch('https://example.com/api/data', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ age: 30 }) }) .then(response => response.[1]())
Use response.json() to convert the response body to a JavaScript object.
Fix the error in the fetch POST request to correctly send JSON data.
fetch('https://example.com/api/data', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: [1] })
The body must be a JSON string, so use JSON.stringify() to convert the object.
Fill both blanks to handle the fetch POST response and catch errors.
fetch('https://example.com/api/data', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ score: 100 }) }) .then(response => response.[1]()) .then(data => console.log(data)) .catch(error => [2])
Use response.json() to parse the response, and console.error(error) to log errors.
Fill all three blanks to create a React Native function that sends a POST request and updates state with the response.
import React, { useState } from 'react'; import { Button, Text, View } from 'react-native'; export default function App() { const [message, setMessage] = useState(''); const sendData = () => { fetch('https://example.com/api/message', { method: '[1]', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ text: 'Hello' }) }) .then(response => response.[2]()) .then(data => setMessage(data.[3])) .catch(error => console.error(error)); }; return ( <View> <Button title="Send" onPress={sendData} /> <Text>{message}</Text> </View> ); }
The method must be POST. Use response.json() to parse the response. Then update state with data.message.