Prompt

How do I validate status codes and JSON payloads in API tests?

Technology / Testing & QA1 observationLast seen Oct 6, 2026

Latest observation

Oct 6, 2026OpenAI APIWeb search: off

To validate status codes and JSON payloads in API tests, you typically check two things:

  1. The HTTP response status
  2. The response body structure and values

1) Validate status codes

Check that the API returns the expected HTTP code, such as:

  • 200 OK for successful GET/PUT requests
  • 201 Created for successful POST requests
  • 204 No Content for successful deletes
  • 400 Bad Request for validation errors
  • 401 Unauthorized / 403 Forbidden for auth issues
  • 404 Not Found for missing resources
  • 500 Internal Server Error for unexpected failures

Example

  • If you call GET /users/1, expect 200
  • If you call POST /users with valid data, expect 201
  • If you call DELETE /users/1, expect 204

2) Validate JSON payloads

Verify that the response body:

  • is valid JSON
  • has the expected fields
  • has correct data types
  • contains expected values
  • does not contain unexpected/malformed data

Example checks

For a response like:

{
  "id": 1,
  "name": "Alice",
  "email": "alice@example.com"
}

You might validate:

  • id is present and is a number
  • name equals "Alice"
  • email is present and matches an email format

3) Common assertions to use

Depending on your test tool/framework, assertions usually look like:

  • Status code
    • assert response.status_code == 200
  • JSON key exists
    • assert "id" in response.json()
  • Exact value
    • assert response.json()["name"] == "Alice"
  • Type check
    • assert isinstance(response.json()["id"], int)
  • Array length
    • assert len(response.json()) > 0

4) Example in Python (requests + pytest)

import requests

def test_get_user():
    response = requests.get("https://api.example.com/users/1")

    assert response.status_code == 200

    data = response.json()
    assert data["id"] == 1
    assert data["name"] == "Alice"
    assert "email" in data

5) Example in JavaScript (fetch + Jest`)

test('get user', async () => {
  const response = await fetch('https://api.example.com/users/1');
  expect(response.status).toBe(200);

  const data = await response.json();
  expect(data.id).toBe(1);
  expect(data.name).toBe('Alice');
  expect(data.email).toBeDefined();
});

6) Good practices

  • Validate both happy path and error cases
  • Check schema/structure, not only exact values
  • Assert critical fields only; avoid overly brittle tests
  • Use JSON schema validation for larger payloads
  • Separate tests for:
    • status code
    • response body
    • headers
    • error handling

7) Schema validation example

If your API has a known contract, use JSON Schema to ensure the payload matches expected structure and types.

Example checks:

  • required fields exist
  • types are correct
  • no extra fields if your contract forbids them

If you want, I can also show you how to do this in Postman, RestAssured, pytest, or Cypress.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as Circuit. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.