qa / api testing
API Testing Tutorial: Seven Negative Cases Run Against a Real Public API
Most API testing tutorials show a 200 response and stop. This one sends bad requests to GitHub's public REST API, records what came back, and turns the results into an automated suite. Every response below was captured in October 2026.
API testing means sending requests directly to a service and checking the responses, with no browser in between. It is faster and more stable than UI testing, and it finds a different class of bug: wrong status codes, missing validation, error bodies that leak internals, and endpoints that accept requests they should refuse. The happy path is the easy part. A tester earns their keep on the requests nobody wrote a story for. To practice on something real, this page uses GitHub's REST API, which needs no account for public data. GitHub's documentation sets the unauthenticated limit at 60 requests per hour per IP address, so every example here is deliberately small.
01 · THE WALKTHROUGH
Seven requests, and what each one proves
The baseline is GET https://api.github.com/repos/octocat/Hello-World, which returned 200 with a JSON body and rate-limit headers (x-ratelimit-limit: 60). Then the failures:
| # | Request | Status | Body or finding |
|---|---|---|---|
| 1 | GET a repository that does not exist | 404 | "message": "Not Found" with a documentation link, as JSON |
| 2 | GET with Authorization: Bearer not-a-real-token | 401 | "message": "Bad credentials". A bad token is refused even on a public endpoint. |
| 3 | POST an issue with no credentials | 401 | "message": "Requires authentication" |
| 4 | DELETE the repository with no credentials | 401 | Same message. Nothing was deleted, and the check ran before any lookup. |
| 5 | GET issues with ?state=bogus | 422 | "message": "Validation Failed" and an errors array naming field state, value bogus, code invalid |
| 6 | GET issues with ?per_page=abc | 200 | The invalid value was silently ignored and a default page returned. Note it: some teams would call this a defect. |
| 7 | GET with Accept: application/xml | 415 | "Unsupported 'Accept' header: 'application/xml'. Must accept 'application/json'." |
Rows 5 and 6 teach the most. They show two different answers to bad input on the same endpoint: reject it with a precise error, or ignore it. Neither is wrong by itself, but a tester's job is to notice, write down which behavior the API has, and check that it matches the documentation. Row 1 also shows a design point: the error is JSON, not an HTML page, so a client can parse it.
02 · MAKING IT REPEATABLE
The same checks as an automated suite
This Playwright test file uses its built-in request fixture, so there is no browser and no extra library. All five tests passed when run.
import { test, expect } from '@playwright/test';
const api = 'https://api.github.com';
test('missing repo returns 404 with a JSON error body', async ({ request }) => {
const res = await request.get(`${api}/repos/octocat/no-such-repo-12345`);
expect(res.status()).toBe(404);
expect(res.headers()['content-type']).toContain('application/json');
expect(await res.json()).toMatchObject({ message: 'Not Found' });
});
test('bad token is 401, not 403 or 404', async ({ request }) => {
const res = await request.get(`${api}/repos/octocat/Hello-World`, {
headers: { Authorization: 'Bearer not-a-real-token' },
});
expect(res.status()).toBe(401);
expect((await res.json()).message).toBe('Bad credentials');
});
test('an invalid enum value is a 422 that names the field', async ({ request }) => {
const res = await request.get(`${api}/repos/octocat/Hello-World/issues?state=bogus`);
expect(res.status()).toBe(422);
expect((await res.json()).errors[0]).toMatchObject({ field: 'state', code: 'invalid' });
});
// plus: POST without credentials is 401, and Accept: application/xml is 415Two cautions when you adapt this. First, tests against a public API you do not own are for learning; they depend on someone else's data and rate limit, so a real project tests its own service in a test environment. Second, assert on the things the contract promises (status, content type, error shape), not on incidental values like the exact text of every message.
03 · WHAT TO CHECK
Reading status codes the way the spec does
RFC 9110, the HTTP semantics standard, defines what each code class means, and MDN's status reference is the readable version. The distinctions below are the ones testers get wrong most often.
| Situation | Expect | Common wrong answer |
|---|---|---|
| No or invalid credentials | 401 | 403, or 200 with an error message in the body |
| Valid credentials, not allowed to do this | 403 | 404 (sometimes deliberate, to hide that a resource exists; the docs should say so) |
| Resource does not exist | 404 | 200 with an empty list or null |
| Well-formed request, invalid values | 400 or 422, with the field named | 500 from an unhandled exception |
| Unsupported media type or Accept | 415 for the request body, 406 for what the client accepts | Ignoring the header |
| Too many requests | 429, usually with a retry hint | Silent slowdown or a 500 |
For every response, also check the content type, the body's shape (field names, types, required fields), that errors do not expose stack traces or file paths, and that repeating an idempotent request (GET, PUT, DELETE) gives the same result. A 500 on bad input is always a bug: the server failed to handle something a client can cause.
04 · BEYOND HAND-WRITTEN CASES
Contracts, generated tests and consumer-driven checks
Once the basics work, three ideas scale them. An OpenAPI description is a machine-readable contract for an API: its paths, parameters and response shapes. The OpenAPI specification is maintained at spec.openapis.org, and at the time of writing the latest document there was version 3.2.1. Tools can validate real responses against it. Schemathesis reads an OpenAPI or GraphQL description and generates test cases from it, aiming to catch bugs before your users do. Pact supports consumer-driven contract testing, where the client team records what it expects from the service and the provider verifies it, which suits microservices that deploy separately.
The tool question is smaller than it looks. Postman is the best-known graphical client and has a learning center. Bruno describes itself on GitHub as an open source IDE for exploring and testing APIs, and keeps collections as files you can commit. REST Assured is a Java library for writing API tests as code. curl and Playwright's request fixture need nothing else installed. Pick any one, because the skill is knowing which requests to send.
05 · SOURCES
Free references and tools
- Rate limits for the GitHub REST API ↗The 60 requests per hour unauthenticated limit and how authenticated limits differ. Read it before you script anything.docs.github.com
- GitHub REST API: repositories ↗The documented behavior to compare your negative-case results against.docs.github.com
- HTTP response status codes (MDN) ↗A readable reference for every status code.developer.mozilla.org
- RFC 9110: HTTP Semantics ↗The standard itself, for settling arguments about what a code means.rfc-editor.org
- API testing with Playwright ↗The request fixture used above, with setup and teardown examples.playwright.dev
- Get started in Postman ↗Postman's own getting-started documentation.learning.postman.com
- Bruno ↗An open source API client whose collections live in your repository.github.com
- REST Assured ↗The Java library for API tests written as code.rest-assured.io
- Schemathesis ↗Generates API test cases from an OpenAPI or GraphQL description.github.com
- Pact documentation ↗Consumer-driven contract testing, explained from the start.docs.pact.io
06 · PRACTICE
Find a behavior nobody documented
curl -i so you see headers. Record status, content type and error shape in a table like the one above, then compare with the documentation. Anything that returns 500, or that behaves differently from the docs, is a finding you can write up. Watch the x-ratelimit-remaining header as you go.07 · FAQ
Frequently asked questions
What is API testing?
API testing sends requests directly to a service's endpoints and checks the responses: status codes, headers and body content. It checks behavior and error handling without a user interface, which makes it faster and more stable than browser testing.
How do I test an API for negative cases?
Send requests that should fail and check they fail correctly: missing or bad credentials, nonexistent resources, invalid values, wrong content types and malformed bodies. Verify the status code, that the error body is parseable and names the problem, and that the server never returns a 500 for input a client can cause.
What tools do I need to learn API testing?
One graphical client such as Postman or Bruno, curl for quick checks, and a code-based option such as Playwright's request fixture or REST Assured for automation. The tool matters less than knowing which requests to send.
What is the difference between 401 and 403?
401 means the request lacks valid credentials, so the server does not know who you are. 403 means the server knows who you are and refuses the action. A bad token on a public endpoint returning 401, as GitHub does, is an example.
Do I need to know how to code to test APIs?
Not to start. You can send requests and write simple assertions in Postman or Bruno without coding. To automate suites and run them in CI, basic programming in JavaScript, Python or Java becomes necessary.