Bird
0
0

You want to send a POST request to an API to create a new user with name 'Anna'. Which of these code snippets correctly sends JSON data in the request body?

hard📝 Application Q8 of 15
Rest API - REST API Fundamentals
You want to send a POST request to an API to create a new user with name 'Anna'. Which of these code snippets correctly sends JSON data in the request body?
Afetch('https://api.example.com/users', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({name: 'Anna'}) })
Bfetch('https://api.example.com/users', { method: 'GET', body: JSON.stringify({name: 'Anna'}) })
Cfetch('https://api.example.com/users', { method: 'POST', body: {name: 'Anna'} })
Dfetch('https://api.example.com/users', { method: 'POST', body: JSON.stringify({name: 'Anna'}) })
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct POST request structure

    POST requests with JSON need 'Content-Type' header and stringified body.
  2. Step 2: Check which option includes header and stringified body

    fetch('https://api.example.com/users', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({name: 'Anna'}) }) includes both header and JSON.stringify for body correctly.
  3. Final Answer:

    Option with method POST, header 'Content-Type: application/json', and stringified body -> Option A
  4. Quick Check:

    POST JSON needs header + JSON.stringify body [OK]
Quick Trick: Always set Content-Type header for JSON POST requests [OK]
Common Mistakes:
MISTAKES
  • Omitting Content-Type header
  • Sending body as object without JSON.stringify
  • Using GET method with body

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes