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?
POST requests with JSON need 'Content-Type' header and stringified body.
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.
Final Answer:
Option with method POST, header 'Content-Type: application/json', and stringified body -> Option A
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
Master "REST API Fundamentals" in Rest API
9 interactive learning modes - each teaches the same concept differently