Skip to content

Instantly share code, notes, and snippets.

@mykeels
Last active March 17, 2017 08:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mykeels/6551493841a934e55e5a0debb2a6be25 to your computer and use it in GitHub Desktop.
Save mykeels/6551493841a934e55e5a0debb2a6be25 to your computer and use it in GitHub Desktop.
Http Response Status Codes in JSON, JavaScript, CSharp, (feel free to add yours), etc.

HTTP Response Status Codes

You've probably heard not to hardcode numbers when writing code. Use constants instead. Let this be your guide when building RESTful APIs.

This is still a work in progress, and I intend to add code for multiple languages (the ones I know). If you want support for your language, feel free to add it.

How to generate Status Codes for your Language

  • Copy the Javascript Code in http-response-codes.js and paste in your browser console

  • Run the following code in the same console to generate for C#

    Object.keys(STATUS_CODES).map(key => `public const int ${key} = ${STATUS_CODES[key]};`).join('\n')
  • Be sure to edit the code to adjust for your language :)

const STATUS_CODES = {
OK: 200,
CREATED: 201,
ACCEPTED: 202,
NON_AUTHORITATIVE_INFORMATION: 203,
NO_CONTENT: 204,
RESET_CONTENT: 205,
PARTIAL_CONTENT: 206,
MULTIPLE_CHOICE: 300,
MOVED_PERMANENTLY: 301,
FOUND: 302,
SEE_OTHER: 303,
NOT_MODIFIED: 304,
UNUSED: 306,
TEMPORARY_REDIRECT: 307,
BAD_REQUEST: 400,
UNAUTHORIZED: 401,
PAYMENT_REQUIRED: 402,
FORBIDDEN: 403,
NOT_FOUND: 404,
METHOD_NOT_ALLOWED: 405,
NOT_ACCEPTABLE: 406,
PROXY_AUTH_REQUIRED: 407,
TIMEOUT: 408,
CONFLICT: 409,
GONE: 410,
LENGTH_REQUIRED: 411,
PRECONDITIONS_FAILED: 412,
REQUEST_ENTITY_TOO_LARGE: 413,
REQUESTED_URI_TOO_LONG: 414,
UNSUPPORTED_MEDIA: 415,
BAD_REQUEST_RANGE: 416,
EXPECTATION_FAILED: 417,
HTTP_UNAVAILABLE_FOR_LEGAL_REASONS: 451,
SERVER_ERROR: 500,
NOT_IMPLEMENTED: 501,
BAD_GATEWAY: 502,
SERVICE_UNAVAILABLE: 503,
GATEWAY_TIMEOUT: 504,
BAD_HTTP_VERSION: 505
}
{
"OK": 200,
"CREATED": 201,
"ACCEPTED": 202,
"NON_AUTHORITATIVE_INFORMATION": 203,
"NO_CONTENT": 204,
"RESET_CONTENT": 205,
"PARTIAL_CONTENT": 206,
"MULTIPLE_CHOICE": 300,
"MOVED_PERMANENTLY": 301,
"FOUND": 302,
"SEE_OTHER": 303,
"NOT_MODIFIED": 304,
"UNUSED": 306,
"TEMPORARY_REDIRECT": 307,
"BAD_REQUEST": 400,
"UNAUTHORIZED": 401,
"PAYMENT_REQUIRED": 402,
"FORBIDDEN": 403,
"NOT_FOUND": 404,
"METHOD_NOT_ALLOWED": 405,
"NOT_ACCEPTABLE": 406,
"PROXY_AUTH_REQUIRED": 407,
"TIMEOUT": 408,
"CONFLICT": 409,
"GONE": 410,
"LENGTH_REQUIRED": 411,
"PRECONDITIONS_FAILED": 412,
"REQUEST_ENTITY_TOO_LARGE": 413,
"REQUESTED_URI_TOO_LONG": 414,
"UNSUPPORTED_MEDIA": 415,
"BAD_REQUEST_RANGE": 416,
"EXPECTATION_FAILED": 417,
"HTTP_UNAVAILABLE_FOR_LEGAL_REASONS": 451,
"SERVER_ERROR": 500,
"NOT_IMPLEMENTED": 501,
"BAD_GATEWAY": 502,
"SERVICE_UNAVAILABLE": 503,
"GATEWAY_TIMEOUT": 504,
"BAD_HTTP_VERSION": 505
}
public enum STATUS_CODES {
OK = 200,
CREATED = 201,
ACCEPTED = 202,
NON_AUTHORITATIVE_INFORMATION = 203,
NO_CONTENT = 204,
RESET_CONTENT = 205,
PARTIAL_CONTENT = 206,
MULTIPLE_CHOICE = 300,
MOVED_PERMANENTLY = 301,
FOUND = 302,
SEE_OTHER = 303,
NOT_MODIFIED = 304,
UNUSED = 306,
TEMPORARY_REDIRECT = 307,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
PAYMENT_REQUIRED = 402,
FORBIDDEN = 403,
NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
NOT_ACCEPTABLE = 406,
PROXY_AUTH_REQUIRED = 407,
TIMEOUT = 408,
CONFLICT = 409,
GONE = 410,
LENGTH_REQUIRED = 411,
PRECONDITIONS_FAILED = 412,
REQUEST_ENTITY_TOO_LARGE = 413,
REQUESTED_URI_TOO_LONG = 414,
UNSUPPORTED_MEDIA = 415,
BAD_REQUEST_RANGE = 416,
EXPECTATION_FAILED = 417,
HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
SERVER_ERROR = 500,
NOT_IMPLEMENTED = 501,
BAD_GATEWAY = 502,
SERVICE_UNAVAILABLE = 503,
GATEWAY_TIMEOUT = 504,
BAD_HTTP_VERSION = 505
}
@shalvah
Copy link

shalvah commented Mar 1, 2017

203 is NON-AUTHORITATIVE INFORMATION.
https://httpstatuses.com/203

@shalvah
Copy link

shalvah commented Mar 1, 2017

How come you omitted 404?

@shalvah
Copy link

shalvah commented Mar 1, 2017

You could also add 451: UNAVAILABLE FOR LEGAL REASONS
https://httpstatuses.com/451

@mykeels
Copy link
Author

mykeels commented Mar 1, 2017

@shalvah Lmao ... 🤣 ... I wonder how I missed that

@mykeels
Copy link
Author

mykeels commented Mar 1, 2017

@shalvah, @allengblack ... fixed 😄

@perfectmak
Copy link

@mykeels how about turning this into a web page, so a user can easily go there, click on his language of choice and select download?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment