QA Interview Hub · Knowledge base · Quizzes · Українська

API › Anatomy of a request and response

What to check in each part

For each part of the request and response (URL, path, query, headers, body, status, time) separate positive and negative cases are written. The order of cases mirrors how the server parses a request: cheap checks (token, format) first, the owner and business rules last. The status is checked exactly, not as "any 4xx", plus the error format and the absence of sensitive fields.

API · Request anatomy

A new endpoint: where do the cases start? Walking through the request parts gives a ready plan.

A ticket arrives: "Test POST /accounts/{id}/transfers". An empty page of cases is scary, and the usual result is "I'll send a valid request, then some invalid one". A better way: take the request from the previous lesson and, for each of its parts and then for each part of the response, ask "what can go wrong here". The table below is exactly such a plan.

PartExampleWhat to check
Base URLhttps://api.bank.test/v1Keep it in a variable {{baseUrl}}, the version is in the address
Path/accounts/42/transfersSomeone else's or a non-existent id gives 403 or 404
Path param42An invalid type (abc) gives 400
Query param?dryRun=falseFilters, pagination, sorting, unknown values
HeadersAuthorization, Content-Type, AcceptNo token gives 401; a wrong Content-Type gives 415
BodyJSON with fieldsRequired fields, types, limits
Status201 CreatedThe code matches the documentation
Response headersContent-Type, LocationContent type, cache, CORS
Response body{ "id": "tr_91", "status": "pending" }Structure, types, values, no extra sensitive fields
Time180 msWithin the SLA

Why these cases: the order of checks on the server

The codes 401, 415, 400 and 422 are not random: they reflect the order in which a server (or the gateway in front of it) processes a request. Cheap checks come first so the database is not used for a request that will be rejected anyway.

A typical order of request processing

1 authentication → 401 2 permission (role, scope) → 403 3 Content-Type → 415 4 JSON parsing → 400 5 field validation → 422 6 owner, business rule → 404/409 7 execution → 2xx
Figure 1. A typical order: the cheaper the check, the earlier it stands. A real service may swap items, so the order of cases is verified against the documentation.

Why this order. Without a token there is no point in reading the body: 401 answers at once. The format (Content-Type, JSON syntax) is checked before the semantic validation, otherwise the server cannot read the fields. Rights to the action (role, scope) are visible in the token, so they are checked early. The resource owner and business rules (409, for instance insufficient funds) are checked last, because they need database state. For someone else's resource 404 is more common than 403, so as not to reveal that such an id exists: this is an approach, not a rule, and it is verified against the contract. The difference between 400 and 422 and other pairs is covered in the lesson "401 vs 403, 400 vs 422, 500 vs 503".

Example: a set of negative cases and the bug it finds

Three quick curl requests, one per part of the request (the shell has BASE and TOKEN set):

# 1. no token
curl -i -X POST "$BASE/v1/accounts/42/transfers" \
  -H "Content-Type: application/json" -d '{"amount": 150}'

# 2. wrong Content-Type
curl -i -X POST "$BASE/v1/accounts/42/transfers" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: text/plain" -d 'amount=150'

# 3. path param of the wrong type
curl -i "$BASE/v1/accounts/abc/transfers" -H "Authorization: Bearer $TOKEN"

Expected: the first gives 401, the second 415, the third 400. Suppose the second returns this:

HTTP/1.1 500 Internal Server Error
Content-Type: text/html

<html><body>NullPointerException at TransferController...</body></html>

That is a bug in two places: status 500 instead of 415 (the server is not prepared for a foreign type), and an HTML body mentioning an exception instead of a JSON error (which is also a leak of internal details). One negative case on the "Headers" part found both. Once the cases are stable they move into a parameterised test, where the table of expectations is separate from the code:

const cases = [
  { name: 'no token', headers: {}, path: '/v1/accounts/42', status: 401 },
  { name: 'id is not a number', headers: AUTH, path: '/v1/accounts/abc', status: 400 },
  { name: 'unknown id', headers: AUTH, path: '/v1/accounts/999999', status: 404 },
];

for (const c of cases) {
  test('GET account: ' + c.name, async ({ request }) => {
    const res = await request.get(c.path, { headers: c.headers });
    expect(res.status()).toBe(c.status);
    expect(res.headers()['content-type']).toContain('json');
  });
}

Every case checks content-type, because an error must also follow the API format. Adding a case means adding a row to the table.

"Status only" versus checking the parts

Typical

Status only

A green test notices neither HTML instead of JSON, nor a missing Location, nor a secret in the body. The client breaks later.

Better

Status, headers, body, time

Each part has its own check. The defect shows up in the test, not in production logs.

Figure 2. The same request can be checked shallowly or fully. The full check costs a few lines.

Limits and alternatives

  • The table describes standard, not guaranteed, behaviour: the API documentation and the contract take priority. If a service returns 404 instead of 403 by design, it is not a bug.
  • Response time in a single run is a weak signal (network, cold cache). It is checked as a rough threshold, while percentiles and load belong to a separate test.
  • Schema validation (OpenAPI, JSON Schema) covers the body structure and types automatically, but does not replace cases on values, permissions and state.
  • UI checks know nothing about headers: some cases from the table can only be done at the API level.

Common mistakes

  • Checking only the positive path. Most defects live in rejected requests: a wrong type, someone else's id, a missing header.
  • Not checking the error format. A valid status with an HTML body breaks a client that expects JSON.
  • Catching "any 4xx". A test status >= 400 lets a swap of 415 for 500 or 404 for 403 through, so the status is checked exactly.
  • Duplicating the base URL and the token in every test. Changing the environment breaks everything at once, so they go into variables or config.

Check yourself

A request with Content-Type: text/plain and valid JSON inside returns 200 and creates the resource. Is this a bug or acceptable flexibility?

Answer

Most likely a bug if the contract requires application/json: the server should answer 415. Silently "accepting everything" hides client mistakes and opens the way for attacks with unexpected types. The documentation has the last word: if it says otherwise, it is acceptable.

Why does the token check come before field validation in the processing order?

Answer

There is no point for an unknown client to have the body read or the database queried. Besides, validation errors reveal the shape of the data and should not be shown without authentication. So 401 first, and only then the content.

In an interview. Junior: I go through the request part by part: URL and path parameters, query, headers, body, and in the response the status, headers, body and time. Each part has its own negative cases, for example an invalid id type gives 400 and a wrong Content-Type gives 415. Middle/Senior: The cases reflect the order in which the server processes a request: authentication, rights, format, syntax, validation, owner and business rules. I check the status exactly, not "any 4xx", and add a check of the error format and the absence of sensitive fields. I keep the cases in a table so a new one costs one row.

More articles in this chapter