Bird
Raised Fist0
Testing Fundamentalstesting~15 mins

Request methods and status codes in Testing Fundamentals - Build an Automation Script

Choose your learning style10 modes available
Verify HTTP GET and POST request methods and status codes
Preconditions (2)
Step 1: Send a GET request to https://jsonplaceholder.typicode.com/posts/1
Step 2: Verify the response status code is 200
Step 3: Verify the response body contains a JSON object with an 'id' field equal to 1
Step 4: Send a POST request to https://jsonplaceholder.typicode.com/posts with JSON body {"title": "foo", "body": "bar", "userId": 1}
Step 5: Verify the response status code is 201
Step 6: Verify the response body contains a JSON object with a non-null 'id' field
✅ Expected Result: GET request returns status 200 and correct post data; POST request returns status 201 and new post id
Automation Requirements - pytest with requests library
Assertions Needed:
Assert response status codes are 200 for GET and 201 for POST
Assert response JSON contains expected fields and values
Best Practices:
Use requests library for HTTP calls
Use pytest assertions for validation
Separate test steps clearly
Handle exceptions if requests fail
Automated Solution
Testing Fundamentals
import requests
import pytest

def test_get_post_and_status_code():
    # Step 1: Send GET request
    get_response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
    # Step 2: Assert status code 200
    assert get_response.status_code == 200, f"Expected 200 but got {get_response.status_code}"
    # Step 3: Assert response JSON has id=1
    json_data = get_response.json()
    assert json_data.get('id') == 1, f"Expected id=1 but got {json_data.get('id')}"

    # Step 4: Send POST request
    post_payload = {"title": "foo", "body": "bar", "userId": 1}
    post_response = requests.post('https://jsonplaceholder.typicode.com/posts', json=post_payload)
    # Step 5: Assert status code 201
    assert post_response.status_code == 201, f"Expected 201 but got {post_response.status_code}"
    # Step 6: Assert response JSON has non-null id
    post_json = post_response.json()
    assert post_json.get('id') is not None, "Expected non-null id in POST response"

This test uses the requests library to send HTTP requests.

First, it sends a GET request to fetch post with id 1 and checks the status code is 200, meaning success.

It then verifies the JSON response contains the expected id field with value 1.

Next, it sends a POST request with a JSON payload to create a new post.

The test asserts the POST response status code is 201, which means resource created successfully.

Finally, it checks the response JSON contains a non-null id field, indicating the new post was assigned an id.

Assertions include helpful messages for easier debugging.

This clear step-by-step approach helps beginners understand request methods and status codes practically.

Common Mistakes - 3 Pitfalls
Using incorrect HTTP method for the action
Not checking the response status code before accessing JSON
Hardcoding expected values without verifying API behavior
Bonus Challenge

Now add data-driven testing with 3 different POST payloads to verify creation with varied data

Show Hint

Practice

(1/5)
1. Which HTTP request method is typically used to retrieve data from a server without changing it?
easy
A. PUT
B. POST
C. DELETE
D. GET

Solution

  1. Step 1: Understand the purpose of HTTP methods

    GET is used to request data without modifying it, while POST, PUT, and DELETE change data on the server.
  2. Step 2: Match method to action

    Since the question asks for retrieving data without changes, GET is the correct method.
  3. Final Answer:

    GET -> Option D
  4. Quick Check:

    Retrieve data = GET [OK]
Hint: GET always fetches data without side effects [OK]
Common Mistakes:
  • Confusing POST with GET for data retrieval
  • Thinking PUT or DELETE retrieve data
  • Assuming GET modifies data
2. Which HTTP status code means the request was successful and the server returned the requested data?
easy
A. 200 OK
B. 404 Not Found
C. 500 Internal Server Error
D. 301 Moved Permanently

Solution

  1. Step 1: Identify success status codes

    HTTP status codes starting with 2xx indicate success. 200 OK means the request succeeded and data is returned.
  2. Step 2: Eliminate error and redirect codes

    404 means not found, 500 means server error, and 301 means redirect. These do not indicate success with data returned.
  3. Final Answer:

    200 OK -> Option A
  4. Quick Check:

    Success code = 200 OK [OK]
Hint: 200 means success and data returned [OK]
Common Mistakes:
  • Choosing 404 thinking it means success
  • Confusing 301 redirect with success
  • Mixing client and server error codes
3. What will be the HTTP status code returned if a client sends a DELETE request to remove a resource successfully?
medium
A. 201 Created
B. 204 No Content
C. 400 Bad Request
D. 403 Forbidden

Solution

  1. Step 1: Understand DELETE success codes

    A successful DELETE request usually returns 204 No Content, meaning the resource was deleted and no content is returned.
  2. Step 2: Eliminate other codes

    201 Created is for resource creation, 400 Bad Request means client error, and 403 Forbidden means no permission.
  3. Final Answer:

    204 No Content -> Option B
  4. Quick Check:

    DELETE success = 204 No Content [OK]
Hint: DELETE success usually returns 204 No Content [OK]
Common Mistakes:
  • Choosing 201 which is for creation
  • Confusing 400 with success
  • Thinking 403 means success
4. A test script sends a POST request to create a new user but receives a 405 Method Not Allowed status. What is the most likely cause?
medium
A. The server does not support the POST method on this URL
B. The user data is invalid
C. The server is down
D. The request timed out

Solution

  1. Step 1: Understand 405 status meaning

    405 Method Not Allowed means the HTTP method used is not supported by the server for the requested URL.
  2. Step 2: Eliminate other causes

    Invalid data usually causes 400 errors, server down causes 5xx errors, and timeout causes no response or 408.
  3. Final Answer:

    The server does not support the POST method on this URL -> Option A
  4. Quick Check:

    405 = Method not allowed [OK]
Hint: 405 means method not allowed on this URL [OK]
Common Mistakes:
  • Thinking 405 means invalid data
  • Confusing 405 with server errors
  • Assuming timeout causes 405
5. During API testing, a PUT request to update a resource returns 409 Conflict. What should the tester check next?
hard
A. If the request method should be GET instead
B. If the request URL is incorrect
C. If the resource already exists and has conflicting changes
D. If the server is overloaded

Solution

  1. Step 1: Understand 409 Conflict meaning

    409 Conflict means the request could not be completed due to a conflict with the current state of the resource, often caused by simultaneous updates.
  2. Step 2: Identify what to check

    The tester should check if the resource exists and if there are conflicting changes that prevent the update.
  3. Final Answer:

    If the resource already exists and has conflicting changes -> Option C
  4. Quick Check:

    409 = Conflict with resource state [OK]
Hint: 409 means conflict with existing resource state [OK]
Common Mistakes:
  • Assuming 409 means wrong URL
  • Thinking 409 means server overload
  • Confusing 409 with method errors
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