Skip to main content

Command Palette

Search for a command to run...

API development response codes

Updated
4 min read
API development response codes
P

Exploring tech world

HTTP response is what gets to a client web browser or application from a server upon HTTP request. This response contains status-code property which provides various states of the received result.

There are five categories of status-code available,

  1. 1XX informational responses

  2. 2XX success responses

  3. 3XX re-directional response

  4. 4XX client error response

  5. 5XX server error response

Status codeDescription
100 ContinueThe initial part of a request has been received and the server is continuing to process it.
102 ProcessingThe Server has received and still processing the request but the response is not available yet.
200 SuccessShows that request has succeeded.
201 CreatedShows that request has succeeded and a new resource has been created.
202 AcceptedShows that request has been received but not completed yet.
204 No contentThe server has satisfied the request but no need to return a response body. The server may return the updated meta information.
300 Multiple choicesThe request has more than one possible response. User-agent or user should choose one of them.
301 Moved permanentlyThe URL of the requested resource has been changed permanently to the URL given by the location headers.
302 FoundThe URL of the requested resource has been changed temporarily to the URL given by the location headers.
303 See otherThe server is directing the client to a different location to retrieve the requested data. This should be done using a GET request.
304 Not modifiedThis shows that the resource has not been modified from the last request made by the client. The Server can send 304 state responses without a body to retrieve data from the cache version of the resource.
307 Temporary redirectThe server is redirecting the client to a different location but should use the same HTTP method for the new request.
400 Bad requestThe server is unable to understand the request due to invalid syntax.
401 UnauthorizedThe request requires user authentication.
402 Payment requiredThis indicates that the requested content is not available until the client makes a payment.
404 Not foundThe requested resource is unable to be found on the server.
408 Request timeoutThe server is showing that it has timed out waiting for the request to complete and the client should retry to retrieve resources.
414 URI is too longThe server is unable to handle the requested URI hence too long. This may occur due to long query strings or path parameters sent by the client.
415 Unsupported media typeThe server indicates that the requested media format is not supported.
431 Request header fields too largeThe server is refusing to process the request hence the request header fields are too large.
500 Internal server errorThe server encountered an unexpected condition that prevented it from fulfilling the request.
501 Not implementedThe server does not support the functionality required to fulfill the request.
502 Bad gatewayThis occurs due to the server acting as a gateway or proxy when it receives an invalid response from an upstream server which may be down or having issues.
504 Gateway timeoutThe server is indicating that it has timed out waiting for the upstream server to complete the request.

Reference: MDN document

Below shows a utility function written in Javascript to handle status codes and corresponding messages.

const RESPONSES = Object.freeze({
  OK: { message: 'Success', statusCode: 200 },
  NOT_MODIFIED: { message: 'Not Modified', statusCode: 304 },
  TEMPORARY_REDIRECT: { message: 'Temporary Redirect', statusCode: 307 },
  REQUEST_TIMEOUT: { message: 'Request Timeout', statusCode: 408 },
  URI_TOO_LONG: { message: 'URI Too Long', statusCode: 414 },
  UNSUPPORTED_MEDIA_TYPE: { message: 'Unsupported Media Type', statusCode: 415 },
  BAD_GATEWAY: { message: 'Bad Gateway', statusCode: 502 },
  GATEWAY_TIMEOUT: { message: 'Gateway Timeout', statusCode: 504 },
  DEFAULT: { message: 'Unknown Server Error', statusCode: 500 },
});

function mapStatusCode(status) {
  return RESPONSES[status] || RESPONSES.DEFAULT;
}

function customResponse(status, data){
    const {message, statusCode} = mapStatusCode(status)
    return {
        statusCode: statusCode,
        body: JSON.stringify({
            message,
            data
        }),
        headers: {
            "Access-Control-Allow-Origin":"*",
            "Access-Control-Allow-Credentials":true
        }
    }
}

the RESPONSES object maps each status code to its corresponding response object and includes a default response object for unknown status codes. The mapStatusCode function looks up in the RESPONSES object based on the provided status code. If the status code is not found in RESPONSES, it falls back to the default response object.

Happy Coding ⚡🎉🌟