0
0
React Nativemobile~20 mins

Fetch API for POST requests in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fetch POST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when this React Native fetch POST request runs?
Consider this React Native code snippet that sends a POST request. What will be the value of response.status if the server accepts the request successfully?
React Native
fetch('https://example.com/api/data', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ name: 'Alice' })
})
.then(response => console.log(response.status))
.catch(error => console.error(error));
A200
B404
C500
D201
Attempts:
2 left
💡 Hint
A successful POST request usually returns a status code in the 200 range.
📝 Syntax
intermediate
1:30remaining
Which option correctly formats the fetch POST request body?
You want to send JSON data in a POST request using fetch in React Native. Which option correctly sets the body property?
Abody: JSON.stringify({ username: 'bob' })
Bbody: { username: 'bob' }
Cbody: JSON.parse('{"username":"bob"}')
Dbody: 'username=bob'
Attempts:
2 left
💡 Hint
The body must be a string when sending JSON data.
lifecycle
advanced
2:30remaining
What is the correct way to handle fetch POST request errors in React Native?
You want to catch network or server errors when sending a POST request with fetch. Which code snippet correctly handles errors?
React Native
fetch('https://api.example.com/login', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ user: 'test' })
})
.then(response => {
  if (!response.ok) throw new Error('Server error');
  return response.json();
})
.then(data => console.log(data))
.catch(error => console.error('Error:', error.message));
AIt catches both network errors and server response errors correctly.
BIt only catches network errors, not server errors.
CIt does not catch any errors because fetch never throws.
DIt catches errors but does not parse JSON correctly.
Attempts:
2 left
💡 Hint
Check how response.ok is used to detect server errors.
navigation
advanced
2:00remaining
After a successful POST request, how to navigate to another screen in React Native?
You send a POST request to login a user. After success, you want to navigate to the Home screen. Which code snippet correctly does this using React Navigation?
React Native
fetch('https://api.example.com/login', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ username: 'user', password: 'pass' })
})
.then(response => response.json())
.then(data => {
  if (data.success) {
    // Navigate to Home
  }
})
.catch(error => console.error(error));
Anavigate('Home');
Bnavigation.navigate('Home');
CNavigation.push('Home');
Dthis.props.navigation.navigate('Home');
Attempts:
2 left
💡 Hint
In functional components, navigation is usually passed as a prop.
🧠 Conceptual
expert
2:30remaining
Why must you include 'Content-Type': 'application/json' header in a fetch POST request?
You send JSON data in a POST request but forget to set the 'Content-Type' header. What is the likely effect?
AThe JSON data will be automatically converted to form data.
BThe fetch request will fail with a syntax error.
CThe server may not parse the JSON body correctly and reject the request.
DThe server will accept the request but ignore the body.
Attempts:
2 left
💡 Hint
The Content-Type header tells the server how to read the request body.