Consider a REST API where you send a POST request to create a new user. The server successfully creates the user and returns a response.
What is the typical HTTP status code you expect in the response?
Think about the status code that indicates a new resource was successfully created.
201 Created is the standard status code returned when a POST request successfully creates a new resource.
You send a POST request to create a new book resource. The server responds with JSON containing the new book's ID and title.
What is the expected JSON response body?
POST /books HTTP/1.1 Host: example.com Content-Type: application/json {"title": "Learn REST"}
The server usually returns the created resource with its new ID.
After creating a resource, the server often returns the full resource including its new unique ID.
Examine the following POST request code snippet. It tries to create a new user but the server returns a 400 Bad Request error.
POST /users HTTP/1.1
Host: api.example.com
Content-Type: application/json
{"username": "", "email": "user@example.com"}What is the most likely cause of the 400 error?
Check the data sent in the request body for required fields.
A 400 Bad Request often means the server rejected the input data. An empty username likely violates validation rules.
When sending a POST request with JSON data to create a resource, which HTTP header must you include to inform the server about the data format?
This header tells the server what format the request body is in.
The Content-Type header specifies the media type of the request body, such as application/json.
A POST request creates a new resource at the server. The server responds with status 201 Created and includes a Location header.
What does the Location header contain?
The Location header tells the client where to find the new resource.
The Location header contains the URL of the newly created resource so the client can access it directly.