<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[CodeGear]]></title><description><![CDATA[CodeGear]]></description><link>https://withcodegear.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 18 Jun 2026 16:59:05 GMT</lastBuildDate><atom:link href="https://withcodegear.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[API development response codes]]></title><description><![CDATA[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,

...]]></description><link>https://withcodegear.hashnode.dev/api-development-response-codes</link><guid isPermaLink="true">https://withcodegear.hashnode.dev/api-development-response-codes</guid><category><![CDATA[api]]></category><category><![CDATA[software development]]></category><category><![CDATA[backend]]></category><dc:creator><![CDATA[Pasan Nadeeja]]></dc:creator><pubDate>Sat, 01 Apr 2023 09:17:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1682269046348/e3cee72a-6666-4539-a945-7a17801ff25b.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>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.</p>
<p>There are five categories of status-code available,</p>
<ol>
<li><p><strong>1XX</strong> informational responses</p>
</li>
<li><p><strong>2XX</strong> success responses</p>
</li>
<li><p><strong>3XX</strong> re-directional response</p>
</li>
<li><p><strong>4XX</strong> client error response</p>
</li>
<li><p><strong>5XX</strong> server error response</p>
</li>
</ol>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Status code</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td>100 Continue</td><td>The initial part of a request has been received and the server is continuing to process it.</td></tr>
<tr>
<td>102 Processing</td><td>The Server has received and still processing the request but the response is not available yet.</td></tr>
<tr>
<td>200 Success</td><td>Shows that request has succeeded.</td></tr>
<tr>
<td>201 Created</td><td>Shows that request has succeeded and a new resource has been created.</td></tr>
<tr>
<td>202 Accepted</td><td>Shows that request has been received but not completed yet.</td></tr>
<tr>
<td>204 No content</td><td>The server has satisfied the request but no need to return a response body. The server may return the updated meta information.</td></tr>
<tr>
<td>300 Multiple choices</td><td>The request has more than one possible response. User-agent or user should choose one of them.</td></tr>
<tr>
<td>301 Moved permanently</td><td>The URL of the requested resource has been changed permanently to the URL given by the location headers.</td></tr>
<tr>
<td>302 Found</td><td>The URL of the requested resource has been changed temporarily to the URL given by the location headers.</td></tr>
<tr>
<td>303 See other</td><td>The server is directing the client to a different location to retrieve the requested data. This should be done using a GET request.</td></tr>
<tr>
<td>304 Not modified</td><td>This 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.</td></tr>
<tr>
<td>307 Temporary redirect</td><td>The server is redirecting the client to a different location but should use the same HTTP method for the new request.</td></tr>
<tr>
<td>400 Bad request</td><td>The server is unable to understand the request due to invalid syntax.</td></tr>
<tr>
<td>401 Unauthorized</td><td>The request requires user authentication.</td></tr>
<tr>
<td>402 Payment required</td><td>This indicates that the requested content is not available until the client makes a payment.</td></tr>
<tr>
<td>404 Not found</td><td>The requested resource is unable to be found on the server.</td></tr>
<tr>
<td>408 Request timeout</td><td>The server is showing that it has timed out waiting for the request to complete and the client should retry to retrieve resources.</td></tr>
<tr>
<td>414 URI is too long</td><td>The 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.</td></tr>
<tr>
<td>415 Unsupported media type</td><td>The server indicates that the requested media format is not supported.</td></tr>
<tr>
<td>431 Request header fields too large</td><td>The server is refusing to process the request hence the request header fields are too large.</td></tr>
<tr>
<td>500 Internal server error</td><td>The server encountered an unexpected condition that prevented it from fulfilling the request.</td></tr>
<tr>
<td>501 Not implemented</td><td>The server does not support the functionality required to fulfill the request.</td></tr>
<tr>
<td>502 Bad gateway</td><td>This 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.</td></tr>
<tr>
<td>504 Gateway timeout</td><td>The server is indicating that it has timed out waiting for the upstream server to complete the request.</td></tr>
</tbody>
</table>
</div><p><em>Reference:</em> <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status"><em>MDN document</em></a></p>
<p>Below shows a utility function written in Javascript to handle status codes and corresponding messages.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> RESPONSES = <span class="hljs-built_in">Object</span>.freeze({
  <span class="hljs-attr">OK</span>: { <span class="hljs-attr">message</span>: <span class="hljs-string">'Success'</span>, <span class="hljs-attr">statusCode</span>: <span class="hljs-number">200</span> },
  <span class="hljs-attr">NOT_MODIFIED</span>: { <span class="hljs-attr">message</span>: <span class="hljs-string">'Not Modified'</span>, <span class="hljs-attr">statusCode</span>: <span class="hljs-number">304</span> },
  <span class="hljs-attr">TEMPORARY_REDIRECT</span>: { <span class="hljs-attr">message</span>: <span class="hljs-string">'Temporary Redirect'</span>, <span class="hljs-attr">statusCode</span>: <span class="hljs-number">307</span> },
  <span class="hljs-attr">REQUEST_TIMEOUT</span>: { <span class="hljs-attr">message</span>: <span class="hljs-string">'Request Timeout'</span>, <span class="hljs-attr">statusCode</span>: <span class="hljs-number">408</span> },
  <span class="hljs-attr">URI_TOO_LONG</span>: { <span class="hljs-attr">message</span>: <span class="hljs-string">'URI Too Long'</span>, <span class="hljs-attr">statusCode</span>: <span class="hljs-number">414</span> },
  <span class="hljs-attr">UNSUPPORTED_MEDIA_TYPE</span>: { <span class="hljs-attr">message</span>: <span class="hljs-string">'Unsupported Media Type'</span>, <span class="hljs-attr">statusCode</span>: <span class="hljs-number">415</span> },
  <span class="hljs-attr">BAD_GATEWAY</span>: { <span class="hljs-attr">message</span>: <span class="hljs-string">'Bad Gateway'</span>, <span class="hljs-attr">statusCode</span>: <span class="hljs-number">502</span> },
  <span class="hljs-attr">GATEWAY_TIMEOUT</span>: { <span class="hljs-attr">message</span>: <span class="hljs-string">'Gateway Timeout'</span>, <span class="hljs-attr">statusCode</span>: <span class="hljs-number">504</span> },
  <span class="hljs-attr">DEFAULT</span>: { <span class="hljs-attr">message</span>: <span class="hljs-string">'Unknown Server Error'</span>, <span class="hljs-attr">statusCode</span>: <span class="hljs-number">500</span> },
});

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">mapStatusCode</span>(<span class="hljs-params">status</span>) </span>{
  <span class="hljs-keyword">return</span> RESPONSES[status] || RESPONSES.DEFAULT;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">customResponse</span>(<span class="hljs-params">status, data</span>)</span>{
    <span class="hljs-keyword">const</span> {message, statusCode} = mapStatusCode(status)
    <span class="hljs-keyword">return</span> {
        <span class="hljs-attr">statusCode</span>: statusCode,
        <span class="hljs-attr">body</span>: <span class="hljs-built_in">JSON</span>.stringify({
            message,
            data
        }),
        <span class="hljs-attr">headers</span>: {
            <span class="hljs-string">"Access-Control-Allow-Origin"</span>:<span class="hljs-string">"*"</span>,
            <span class="hljs-string">"Access-Control-Allow-Credentials"</span>:<span class="hljs-literal">true</span>
        }
    }
}
</code></pre>
<p>the <code>RESPONSES</code> object maps each status code to its corresponding response object and includes a default response object for <code>unknown</code> status codes. The <code>mapStatusCode</code> function looks up in the <code>RESPONSES</code> object based on the provided status code. If the status code is not found in <code>RESPONSES</code>, it falls back to the default response object.</p>
<p>Happy Coding ⚡🎉🌟</p>
]]></content:encoded></item></channel></rss>