What is HTTP? How to choose API methods, headers, and status codes
Learn how HTTP methods express intent, how `Accept` differs from `Content-Type`, and how status codes and conditional requests guide safe API updates.
HTTP is a shared set of rules clients and servers use to exchange web resources. For API work, it is not enough to know that a request gets a response: you also need to understand what a method intends, how headers describe message content, and how a status code guides the next step. This article focuses on the semantics in RFC 9110 (2022), not the order messages travel over the network or the wire framing used by HTTP/2 and HTTP/3.
How are a resource and a representation different?
In HTTP, a resource is something a client interacts with through a URI. It does not have to be the same thing as one database row or file inside the server. A representation is content that expresses that resource in a particular form. The same order resource, for example, can be represented as JSON or HTML. The same resource may have different representations for language, media format, or content coding.
Headers help describe the distinction. A request's Accept tells the server which response formats the client prefers. Content-Type describes the actual format of a message body. The response can also include Content-Type so the client knows how to interpret its body. Sending Accept: application/json does not guarantee that the server returns JSON; check the actual response status and Content-Type.
Method intent matters more than the method name
A method expresses what the client intends to do to the target resource. Its standardized meaning is a guide for API design and client behavior, but each resource determines which methods it allows.
| Method | Standard intent | Safety and idempotency | When to use it |
|---|---|---|---|
GET | Retrieve the current representation | Safe and idempotent | Read a resource. Do not hide a state-changing action in URL parameters. |
HEAD | Check the metadata a GET would return, without its response body | Safe and idempotent | Inspect resource headers without downloading the full body. |
POST | Have the target resource process the request content | Generally unsafe and non-idempotent | Submit work or create a subordinate resource when the server determines how to process it. |
PUT | Replace the target resource's current representation with the request content | Unsafe but idempotent | Set or replace a representation when the target URI is known. |
DELETE | Request removal of the target resource's current association | Unsafe but idempotent | Express deletion intent. Whether underlying data is retained or removed depends on the resource implementation. |
Safe means the method's defined intent is essentially read-only. Idempotent means repeating an identical request has the same intended effect on the server as applying it once. Idempotency does not guarantee identical responses or no side effects such as logging. For example, DELETE is unsafe because it requests a change, but it is idempotent because repeating the deletion has the same intended final effect.
This distinction matters for retries. A client can consider retrying an idempotent request such as PUT if the connection fails before it receives a response. A POST, however, may have been processed even if only the response was lost; retrying automatically can create or process something twice. Check whether the API documents a duplicate-prevention contract. If you cannot determine the outcome, do not blindly resend the request.
Distinguish Accept from Content-Type
Accept means “these are formats I prefer to receive.” Content-Type means “this message body is actually in this format.” For example, a request that creates an order in JSON uses Content-Type: application/json for its request body; the client can also send Accept: application/json if it prefers a JSON response. The server may choose a different representation according to its supported formats or policy, so check the response's actual Content-Type too.
415 Unsupported Media Type may mean that the server cannot process the request body's format or encoding for that method and resource. Compare the request's Content-Type with the formats the API documents. 406 Not Acceptable can be returned when the server is unwilling to provide a representation matching the request's preferences. A server may still return a default format, so do not infer the response format from Accept alone.
Example: create an order and update it safely
The following is a hypothetical API for explanation. Check the actual API documentation for exact paths, allowed methods, required fields, and error responses.
A client requests order creation:
POST /orders HTTP/1.1
Host: shop.example.com
Accept: application/json
Content-Type: application/json
{"itemId":"sku-42","quantity":1}If the server creates the order, it can respond with 201 Created and the location of the new resource:
HTTP/1.1 201 Created
Location: /orders/ord-1042
Content-Type: application/json
{"id":"ord-1042","itemId":"sku-42","quantity":1,"status":"pending"}Location points to the new order resource, and the response Content-Type says the body is JSON. If the connection drops before this response arrives, the order may already exist. Unless the API documents a safe retry method, do not immediately repeat the same POST; look up the result or follow the provider's documented verification process.
Now suppose the client retrieves the order and asks for JSON:
GET /orders/ord-1042 HTTP/1.1
Host: shop.example.com
Accept: application/jsonSuppose the response includes ETag: "v7". This tag is a validator that identifies a representation version. Before changing the quantity, the client can conditionally replace the full representation only if the version it last saw is still current:
PUT /orders/ord-1042 HTTP/1.1
Host: shop.example.com
If-Match: "v7"
Content-Type: application/json
Accept: application/json
{"id":"ord-1042","itemId":"sku-42","quantity":2,"status":"pending"}If the current tag matches, the server can apply the replacement and return a success response without a body, such as 204 No Content. If another client changed the order and the tag no longer matches, the server can reject the update with 412 Precondition Failed. Retrieve the latest order, compare the changes, and prepare a new representation. Resending the same content without the condition could overwrite another client's update.
Status codes point to the next check
Status codes classify the response; they do not replace details in its body and headers. 2xx indicates success, 3xx a further redirect or choice, 4xx a request or access condition to check, and 5xx a server problem while processing. Read the specific code in the context of the method.
`201 Created`: a new resource resulted from the request. Check `Location` for it.
`204 No Content`: processing succeeded, but there is no response body. Do not try to parse an empty body as JSON.
`401 Unauthorized`: authentication may be missing or invalid; check the authentication flow.
`403 Forbidden`: the server recognizes the requester but does not allow access.
`404 Not Found`: the target resource was not found. Some servers also use this status to conceal a resource the requester cannot access.
`405 Method Not Allowed`: the method is recognized but not allowed for this resource. If the response includes `Allow`, use it as a guide to permitted methods.
`412 Precondition Failed`: a condition such as `If-Match` did not match current resource state, so the operation was not applied. Read the latest state and reevaluate the condition.
`415 Unsupported Media Type`: the request content format or encoding may not be supported for this operation.
HTTP defines shared semantics, but it does not specify how a service implements each resource or error. Also check the API's documentation for allowed methods, field rules, authentication policy, and error bodies.
Sources
[RFC 9110: HTTP Semantics](https://www.rfc-editor.org/rfc/rfc9110.html)
[MDN: HTTP request methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods)
[MDN: HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers)
[MDN: Representation header](https://developer.mozilla.org/en-US/docs/Glossary/Representation_header)