Bird
Raised Fist0
Rest APIprogramming~20 mins

POST for creating resources in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
REST POST Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:00remaining
What is the HTTP status code returned after a successful POST request?

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?

A200 OK
B404 Not Found
C500 Internal Server Error
D201 Created
Attempts:
2 left
💡 Hint

Think about the status code that indicates a new resource was successfully created.

❓ Predict Output
intermediate
1:30remaining
What is the response body after creating a resource with POST?

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?

Rest API
POST /books HTTP/1.1
Host: example.com
Content-Type: application/json

{"title": "Learn REST"}
A{"id": 101, "title": "Learn REST"}
B{"error": "Missing title"}
C{"status": "success"}
D{}
Attempts:
2 left
💡 Hint

The server usually returns the created resource with its new ID.

🔧 Debug
advanced
2:00remaining
Why does this POST request return 400 Bad Request?

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?

AThe email field is missing
BThe Content-Type header is missing
CThe username field is empty, violating validation rules
DThe HTTP method should be GET instead of POST
Attempts:
2 left
💡 Hint

Check the data sent in the request body for required fields.

🧠 Conceptual
advanced
1:00remaining
Which header is important to include in a POST request with JSON data?

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?

AContent-Type
BAuthorization
CAccept-Encoding
DCache-Control
Attempts:
2 left
💡 Hint

This header tells the server what format the request body is in.

❓ Predict Output
expert
1:30remaining
What is the value of the Location header after a successful POST?

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?

AThe URL of the POST endpoint
BThe URL of the newly created resource
CThe client's IP address
DThe server's root URL
Attempts:
2 left
💡 Hint

The Location header tells the client where to find the new resource.

Practice

(1/5)
1. What is the primary purpose of the POST method in REST APIs?
easy
A. To create a new resource on the server
B. To retrieve data from the server
C. To update an existing resource completely
D. To delete a resource from the server

Solution

  1. Step 1: Understand HTTP methods

    HTTP methods have specific roles: GET retrieves, POST creates, PUT updates, DELETE removes.
  2. Step 2: Identify POST's role

    POST sends data to the server to create a new resource, unlike GET which only fetches data.
  3. Final Answer:

    To create a new resource on the server -> Option A
  4. Quick Check:

    POST = Create resource [OK]
Hint: POST is for sending data to create something new [OK]
Common Mistakes:
  • Confusing POST with GET which only fetches data
  • Thinking POST updates existing resources fully
  • Assuming POST deletes resources
2. Which of the following is the correct way to send data in a POST request to create a resource?
easy
A. Include data in the URL query parameters
B. Send data in the request body as JSON
C. Send data as HTTP headers
D. Send data as a cookie

Solution

  1. Step 1: Identify where POST data goes

    POST requests send data in the request body, not in URL or headers.
  2. Step 2: Recognize common data format

    JSON is the standard format for sending structured data in the body for REST APIs.
  3. Final Answer:

    Send data in the request body as JSON -> Option B
  4. Quick Check:

    POST data = request body JSON [OK]
Hint: POST data goes in the body, not URL or headers [OK]
Common Mistakes:
  • Putting data in URL query instead of body
  • Trying to send data via headers
  • Using cookies to send creation data
3. Given this Python code using the requests library, what will be the HTTP status code if the resource is created successfully?
import requests
url = 'https://api.example.com/items'
data = {'name': 'Book', 'price': 12.99}
response = requests.post(url, json=data)
print(response.status_code)
medium
A. 200
B. 404
C. 201
D. 500

Solution

  1. Step 1: Understand POST success codes

    When a resource is created, the server usually returns HTTP status 201 Created.
  2. Step 2: Analyze the code behavior

    The code sends a POST request with JSON data; if successful, response.status_code will be 201.
  3. Final Answer:

    201 -> Option C
  4. Quick Check:

    POST success = 201 Created [OK]
Hint: 201 means resource created successfully [OK]
Common Mistakes:
  • Confusing 200 OK with 201 Created
  • Assuming 404 means success
  • Thinking 500 means success
4. You wrote this POST request code but get a 400 Bad Request error. What is the likely problem?
import requests
url = 'https://api.example.com/users'
data = {'username': 'alice'}
response = requests.post(url, data=data)
print(response.status_code)
medium
A. The data should be sent as JSON, not form data
B. The URL is incorrect for POST requests
C. POST requests cannot send data
D. The server does not support HTTPS

Solution

  1. Step 1: Check data sending method

    Using data=data sends form-encoded data, but API expects JSON for POST creation.
  2. Step 2: Fix data format

    Use json=data in requests.post to send JSON body, which the server accepts.
  3. Final Answer:

    The data should be sent as JSON, not form data -> Option A
  4. Quick Check:

    Send JSON with json= parameter [OK]
Hint: Use json= not data= to send JSON body [OK]
Common Mistakes:
  • Sending form data instead of JSON
  • Assuming URL is wrong without checking
  • Thinking POST can't send data
  • Ignoring HTTPS requirement
5. You want to create a new blog post resource with title and content using a POST request. Which of these JSON payloads correctly represents the data to send in the request body?
hard
A. ["title", "My Day", "content", "It was sunny."]
B. "title=My Day&content=It was sunny."
C. {"My Day": "title", "It was sunny.": "content"}
D. {"title": "My Day", "content": "It was sunny."}

Solution

  1. Step 1: Understand JSON object structure

    JSON objects use key-value pairs with keys as strings and values as data.
  2. Step 2: Identify correct JSON format

    {"title": "My Day", "content": "It was sunny."} correctly uses keys "title" and "content" with their string values in an object.
  3. Final Answer:

    {"title": "My Day", "content": "It was sunny."} -> Option D
  4. Quick Check:

    JSON object with keys and values = {"title": "My Day", "content": "It was sunny."} [OK]
Hint: JSON data must be key-value pairs inside braces {} [OK]
Common Mistakes:
  • Using JSON arrays instead of objects
  • Swapping keys and values
  • Sending URL-encoded string instead of JSON
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard