What is HTTP Code
HTTP Code is simply a numeric value used to exchange information between Server and Client. HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped into five classes:
- Informational responses ( 100 – 199 )
- Successful responses ( 200 – 299 )
- Redirection messages ( 300 – 399 )
- Client error responses ( 400 – 499 )
- Server error responses ( 500 – 599 )
Information responses
100 Continue
Response indicates that the client should continue the request or ignore the response if the request is already finished.
101 Switching Protocols
Response to an Upgrade request header from the client and indicates the protocol the server is switching to.
102 Processing
Indicates that the server has received and is processing the request, but no response is available yet.
Successful responses
200 OK
Request succeeded. The result meaning of "success" depends on the HTTP method,
- GET The resource has been fetched and transmitted in the message body.
- HEAD The representation headers are included in the response without any message body.
- PUT or POST The resource describing the result of the action is transmitted in the message body.
- TRACE The message body contains the request message as received by the server.
201 Created
Create/POST Request succeeded, and a new resource was created as a result. This is typically the response sent after POST requests, or some PUT requests.
202 Accepted
Request has been received but not yet acted upon. It is noncommittal since there is no way in HTTP to later send an asynchronous response indicating the outcome of the request.
204 No Content
No content to send for this request, but the headers may be useful.
205 Reset Content
User agent to reset the document which sent this request.
206 Partial Content
Response code is used when the Range header is sent from the client to request only part of a resource.
Redirection messages
307 Temporary Redirect
Server Response to direct the client to get the requested resource at another URI with same method that was used in the prior request. This has the same semantics as the 302 Found HTTP response code, with the exception that the user agent must not change the HTTP method used: if a POST was used in the first request, a POST must be used in the second request.
308 Permanent Redirect
This means that the resource is now permanently located at another URI, specified by the Location: HTTP Response header. This has the same semantics as the 301 Moved Permanently HTTP response code, with the exception that the user agent must not change the HTTP method used: if a POST was used in the first request, a POST must be used in the second request.
Client error responses
400 Bad Request
Server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
401 Unauthorized
Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response.
402 Payment Required
Response code is reserved for future use. The initial aim for creating this code was using it for digital payment systems, however this status code is used very rarely and no standard convention exists.
403 Forbidden
Client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server.
404 Not Found
Server can not find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 Forbidden to hide the existence of a resource from an unauthorized client. This response code is probably the most well known due to its frequent occurrence on the web.
405 Method Not Allowed
Request method is known by the server but is not supported by the target resource. For example, an API may not allow calling DELETE to remove a resource.
406 Not Acceptable
Response is sent when the webserver, after performing server-driven content negotiation, doesn't find any content that conforms to the criteria given by the user agent.
407 Proxy Authentication Required
This is similar to 401 Unauthorized but authentication is needed to be done by a proxy.
408 Request Timeout
Response is sent on an idle connection by some servers, even without any previous request by the client. It means that the server would like to shut down this unused connection.
This response is used much more since some browsers, like Chrome, Firefox 27, or IE9, use HTTP pre-connection mechanisms to speed up surfing. Also note that some servers merely shut down the connection without sending this message.
Server error responses
500 Internal Server Error
Server has encountered a situation it does not know how to handle.
501 Not ImplementedRequest method is not supported by the server and cannot be handled. The only methods that servers are required to support (and therefore that must not return this code) are GET and HEAD .
502 Bad Gateway
Error response means that the server, while working as a gateway to get a response needed to handle the request, got an invalid response.
503 Service Unavailable
Server is not ready to handle the request. Common causes are a server that is down for maintenance or that is overloaded. Note that together with this response, a user-friendly page explaining the problem should be sent. This response should be used for temporary conditions and the Retry-After HTTP header should, if possible, contain the estimated time before the recovery of the service. The webmaster must also take care about the caching-related headers that are sent along with this response, as these temporary condition responses should usually not be cached.
504 Gateway Timeout
Error response is given when the server is acting as a gateway and cannot get a response in time.
In .net WebAPI we have a class called StatusCodes Class which has predefined constants to send as reply in web API responses.
Field |
Code |
Status100Continue |
100 |
Status101SwitchingProtocols |
101 |
Status102Processing |
102 |
Status200OK |
200 |
Status201Created |
201 |
Status202Accepted |
202 |
Status203NonAuthoritative |
203 |
Status204NoContent |
204 |
Status205ResetContent |
205 |
Status206PartialContent |
206 |
Status207MultiStatus |
207 |
Status208AlreadyReported |
208 |
Status226IMUsed |
226 |
Status300MultipleChoices |
300 |
Status301MovedPermanently |
301 |
Status302Found |
302 |
Status303SeeOther |
303 |
Status304NotModified |
304 |
Status305UseProxy |
305 |
Status306SwitchProxy |
306 |
Status307TemporaryRedirect |
307 |
Status308PermanentRedirect |
308 |
Status400BadRequest |
400 |
Status401Unauthorized |
401 |
Status402PaymentRequired |
402 |
Status403Forbidden |
403 |
Status404NotFound |
404 |
Status405MethodNotAllowed |
405 |
Status406NotAcceptable |
406 |
Status408RequestTimeout |
408 |
Status409Conflict |
409 |
Status410Gone |
410 |
Status411LengthRequired |
411 |
Status412PreconditionFailed |
412 |
Status413PayloadTooLarge |
413 |
Status413RequestEntityToo |
413 |
Status414RequestUriTooLong |
414 |
Status414UriTooLong |
414 |
Status415UnsupportedMedia |
415 |
Status416RangeNotSatisfiable |
416 |
Status416RequestedRangeNot |
416 |
Status417ExpectationFailed |
417 |
Status418ImATeapot |
418 |
Status419Authentication |
419 |
Status421MisdirectedRequest |
422 |
Status422UnprocessableEntity |
422 |
Status423Locked |
423 |
Status424FailedDependency |
424 |
Status426UpgradeRequired |
426 |
Status428Precondition |
428 |
Status429TooManyRequests |
429 |
Status431RequestHeaderFields |
431 |
Status500InternalServerError |
500 |
Status501NotImplemented |
501 |
Status502BadGateway |
502 |
Status503ServiceUnavailable |
503 |
Status504GatewayTimeout |
504 |
Status505HttpVersion |
505 |
Status506VariantAlso |
506 |
Status507InsufficientStorage |
507 |
Status508LoopDetected |
508 |
Status510NotExtended |
510 |
Status511Network AuthenticationRequired |
511 |
Conclusion
In Web API response it is very important to handle appropriate response codes to enhance the user experience on the website. This article gives an extensive list of Status codes that can be handled in the web API responses