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

API › Anatomy of a request and response

What a request and a response consist of

An HTTP request consists of a method, a URL, headers and a body, and a response of a status code, headers and a body. Each part is a separate source of test cases, so headers (Location, Content-Type) and body field types are checked as well as the status. The path identifies a resource, the query refines the listing, and secrets go in Authorization, because URLs end up in logs.

API · Request anatomy

A test checks only for status 201. What else in this response can be broken?

A developer says: "The endpoint works, it returns 201." The test is green, yet the mobile client cannot open the transfer it just created: the response has no Location, and the amount came back as the string "150". The status was right, the other parts of the message were broken. To check everything you need to know what a request and a response consist of, and to see each part as a separate source of test cases.

Below: the parts of an HTTP exchange, why it is built this way, and how to base tests on it.

How it is built

Per RFC 9110, an HTTP message always has three layers: a start line, headers ("name: value" pairs) and an optional body. In a request the start line holds the method and the target (a path with a query), in a response it holds the status code. Here is a complete request that creates a transfer:

POST /v1/accounts/42/transfers?dryRun=false HTTP/1.1
Host: api.bank.test
Authorization: Bearer eyJhbGciOi...
Content-Type: application/json
Idempotency-Key: 7f3c9a10-5d2e-4b8a-9c11-0e6f2a7b1d34

{ "toAccount": "UA21...", "amount": 150.00, "currency": "UAH" }

The request has four parts: the method (POST, what to do), the URL (base URL with a version, the path to the resource, the query), the headers (metadata: who you are, the body format) and the body (data). The response has three parts:

HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Location: /v1/transfers/tr_91
X-Request-Id: 5b0e7c2a

{ "id": "tr_91", "status": "pending", "amount": 150.00 }

Parts of a request and a response

RequestResponse 1 method 2 URL 3 headers 4 body 5 status code 6 headers 7 body
Figure 1. Seven parts, each of which can be right or broken independently of the others. The status code is highlighted because it is checked most often, and that is why the rest gets forgotten.

Why it is designed this way. Splitting a message into a start line, headers and a body lets intermediaries (proxies, gateways, caches) work with the metadata alone, without parsing the body. Content-Type says how to read the body, Content-Length or Transfer-Encoding says where it ends. So a mistake in a header breaks body parsing even when the body itself is correct.

Path or query. The path points to a specific resource (/users/42), the query refines the result (/users?status=active&page=2). If the resource loses its meaning without the parameter, it is a path. If it is a selection option (filter, sort, page), it is a query.

Example: from "status only" to checking the parts

Before: expect(res.status()).toBe(201) and nothing else. A test like that does not catch the missing Location or the string amount. After: we check every part of the response that belongs to the contract.

import { test, expect } from '@playwright/test';

test('transfer: response parts are complete', async ({ request }) => {
  const res = await request.post('/v1/accounts/42/transfers', {
    headers: { 'Idempotency-Key': crypto.randomUUID() },
    data: { toAccount: 'UA21...', amount: 150, currency: 'UAH' },
  });
  expect(res.status()).toBe(201);
  expect(res.headers()['content-type']).toContain('application/json');
  expect(res.headers()['location']).toMatch(/\/transfers\/tr_\w+$/);
  const body = await res.json();
  expect(body.status).toBe('pending');
  expect(body.amount).toBe(150); // a string "150" would fail here
});

The first expect catches a wrong status, the second and third check headers (the client needs Location to reach the new resource), the last two check the body: the value and the type in one expression, because toBe(150) does not let the string "150" through. The request is built from parts as well: Idempotency-Key is a header, the data is the body, while the base URL and the token live in the configuration, not in the test.

Limits and alternatives

  • Not everything in a request is mandatory. GET and DELETE usually have no body, and a server may reject or ignore a body that has no meaning (RFC 9110 does not forbid it, but gives it no defined semantics).
  • Header names are case-insensitive: Content-Type and content-type mean the same. Clients (Playwright, fetch) return names in lower case, so a test reads headers()['content-type'].
  • Not all of it is "pure" HTTP: the protocol version (HTTP/1.1, HTTP/2) changes the transport but not the semantics of the parts. An API-level test usually does not see it.
  • In Postman or Insomnia the same parts are the Params, Headers and Body tabs. The principle is the same.

Common mistakes

  • Checking only the status. Broken headers, field types or extra data go unnoticed, and the client fails in production.
  • Hiding an id in the query instead of the path (/transfers?id=91), or a filter in the path. It makes caching and reading logs harder, so it is a remark about the API design worth raising.
  • Putting a token in the URL. The query ends up in proxy logs and browser history, so secrets go in the Authorization header.
  • Forgetting Content-Type in the request. The server does not know how to read the body and answers 415 or, worse, silently reads it wrongly.

Check yourself

The response is 201 and the body is right, but the mobile client cannot see the created resource. Which parts of the response should you check first?

Answer

The headers: first of all Location (the URL of the new resource) and Content-Type. The status and the body can be perfect while the client relies on a header that is missing.

A filter by payment status: /payments/pending or /payments?status=pending?

Answer

The query. pending does not identify a resource, it narrows a collection, and /payments makes sense without it. The path is kept for resource identifiers.

In an interview. Junior: A request consists of a method, a URL, headers and a body, a response of a status code, headers and a body. A path parameter identifies a resource, a query filters or paginates the result. Middle/Senior: Each part is a separate source of cases: in a create test I check not only 201 but also Location, Content-Type and the types of the body fields. Headers decide how the body is read, so a header mistake breaks the client even with a correct body. Secrets go in a header, not in the URL, because the URL lands in logs.

More articles in this chapter