Reference article · Programming
HTTP and web APIs: requests, responses and status codes
An API exposes operations another program can use. Many web APIs use HTTP: clients send requests to URLs and servers return responses. HTTP also delivers pages and other resources.
Request components
A request includes a method, target and headers, and can carry a body. GET requests a resource representation; POST submits data for processing. The API contract determines each operation’s precise meaning.
Headers and body
Content-Type describes the transmitted body; Accept expresses acceptable response formats. HTTP carries JSON, HTML, text and other formats. This exchange is illustrative: your server would need to implement /api/articles.
GET /api/articles HTTP/1.1
Host: example.test
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
{"articles":[{"id":1,"title":"HTML"}]}Status families
- 2xx means success; 200 indicates a successful response and 201 indicates creation.
- 3xx covers redirection and related states; 304 indicates a cached representation is unchanged.
- 4xx concerns the request: 400 is a bad request, 401 lacks valid credentials, 403 refuses access and 404 indicates a missing resource.
- 5xx concerns server or intermediary errors; 500 means an internal server error.
Not every response carries data
204 has no content to return, so do not parse an empty JSON body. Success also does not guarantee the structure your application expects; check required fields.
Investigate a request
- Open the browser Network panel and repeat the action.
- Check URL, method, status and headers before the body.
- Compare with the API documentation and distinguish HTTP refusal from connection failure.
References
Related articles
- Fundamentals: variables, conditions and loops
- Semantic HTML and page structure
- CSS: box model, Flexbox and Grid
- JavaScript: functions, parameters and scope
- JavaScript: arrays, objects and data transformations
- DOM and events: connect JavaScript to HTML
- Promises, async/await and fetch requests
- JSON: format, parsing and data validation
- Git: changes, commits, branches and remotes
- Node.js and npm: project scripts and dependencies
- Debugging and tests: finding and checking errors
- Colour formats: HEX, RGB and HSL
- UUID identifiers
- Random values in JavaScript
