Javascript http status codes

HTTP Response Status Codes

Without a library that provides you with all the HTTP status codes in a single box, creating APIs has never been simple. Because of this, I developed an appealing, adaptable, and abstracted package that includes all HTTP API status response codes that are listed in Request for Comments (RFC).

Usage

 const express = require('express'); const  Response > = require('http-status-codez'); const User = require('./models/user'); const app = express(); app.get('/api/v1/users', catchAsync(async (req, res, next) =>  const users = await User.find(); res.status(Response.HTTP_OK).json( status: 'success', results: users.length, data:  users, >, >); >)); app.get('/api/v1/users/:id', catchAsync(async (req, res, next) =>  const user = await User.findById(req.params.id); if (!user)  return next( new AppError('User not found with that ID', Response.HTTP_NOT_FOUND); ); > res.status(Response.HTTP_OK).json( status: 'success', data:  user, >, >); 

Implementation Description

Code Instance Properties Phrase
100 HTTP_CONTINUE Continue
101 HTTP_SWITCHING_PROTOCOLS Switching Protocols
102 HTTP_PROCESSING Processing
103 HTTP_EARLY_HINTS Early Hints
200 HTTP_OK OK
201 HTTP_CREATED Created
202 HTTP_ACCEPTED Accepted
203 HTTP_NON_AUTHORITATIVE_INFORMATION Non Authoritative Information
204 HTTP_NO_CONTENT No Content
205 HTTP_RESET_CONTENT Reset Content
206 HTTP_PARTIAL_CONTENT Partial Content
207 HTTP_MULTI_STATUS Multi-Status
208 HTTP_ALREADY_REPORTED Already Reported
226 HTTP_IM_USED IM Used
300 HTTP_MULTIPLE_CHOICES Multiple Choices
301 HTTP_MOVED_PERMANENTLY Moved Permanently
302 HTTP_FOUND Found
303 HTTP_SEE_OTHER See Other
304 HTTP_NOT_MODIFIED Not Modified
305 HTTP_USE_PROXY Use Proxy
306 HTTP_RESERVED Reserved
307 HTTP_TEMPORARY_REDIRECT Temporary Redirect
308 HTTP_PERMANENTLY_REDIRECT Permanent Redirect
400 HTTP_BAD_REQUEST Bad Request
401 HTTP_UNAUTHORIZED Unauthorized
402 HTTP_PAYMENT_REQUIRED Payment Required
403 HTTP_FORBIDDEN Forbidden
404 HTTP_NOT_FOUND Not Found
405 HTTP_METHOD_NOT_ALLOWED Method Not Allowed
406 HTTP_NOT_ACCEPTABLE Not Acceptable
407 HTTP_PROXY_AUTHENTICATION_REQUIRED Proxy Authentication Required
408 HTTP_REQUEST_TIMEOUT Request Timeout
409 HTTP_CONFLICT Conflict
410 HTTP_GONE Gone
411 HTTP_LENGTH_REQUIRE Length Required
412 HTTP_PRECONDITION_FAILED Precondition Failed
413 HTTP_REQUEST_ENTITY_TOO_LARGE Request Entity Too Large
414 HTTP_REQUEST_URI_TOO_LONG Request-URI Too Long
415 HTTP_UNSUPPORTED_MEDIA_TYPE Unsupported Media Type
416 HTTP_REQUESTED_RANGE_NOT_SATISFIABLE Requested Range Not Satisfiable
417 HTTP_EXPECTATION_FAILED Expectation Failed
418 HTTP_I_AM_A_TEAPOT I’m a teapot
419 HTTP_INSUFFICIENT_SPACE_ON_RESOURCE Insufficient Space on Resource
420 HTTP_METHOD_FAILURE Method Failure
421 HTTP_MISDIRECTED_REQUEST Misdirected Request
422 HTTP_UNPROCESSABLE_ENTITY Unprocessable Entity
423 HTTP_LOCKED Locked
424 HTTP_FAILED_DEPENDENCY Failed Dependency
425 HTTP_TOO_EARLY Too Early
426 HTTP_UPGRADE_REQUIRED Upgrade Required
428 HTTP_PRECONDITION_REQUIRED Precondition Required
429 HTTP_TOO_MANY_REQUESTS Too Many Requests
431 HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE Request Header Fields Too Large
451 HTTP_UNAVAILABLE_FOR_LEGAL_REASONS Unavailable For Legal Reasons
500 HTTP_INTERNAL_SERVER_ERROR Internal Server Error
501 HTTP_NOT_IMPLEMENTED Not Implemented
502 HTTP_BAD_GATEWAY Bad Gateway
503 HTTP_SERVICE_UNAVAILABLE Service Unavailable
504 HTTP_GATEWAY_TIMEOUT Gateway Timeout
505 HTTP_VERSION_NOT_SUPPORTED HTTP Version Not Supported
506 HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL Variant Also Negotiates
507 HTTP_INSUFFICIENT_STORAGE Insufficient Storage
508 HTTP_LOOP_DETECTED Loop Detecte
5010 HTTP_NOT_EXTENDED Not Extended
511 HTTP_NETWORK_AUTHENTICATION_REQUIRED Network Authentication Required

Источник

Response: status property

The status read-only property of the Response interface contains the HTTP status codes of the response.

For example, 200 for success, 404 if the resource could not be found.

Value

An unsigned short number. This is one of the HTTP response status codes.

Examples

In our Fetch Response example (see Fetch Response live) we create a new Request object using the Request() constructor, passing it a JPG path. We then fetch this request using fetch() , extract a blob from the response using Response.blob , create an object URL out of it using URL.createObjectURL , and display this in an .

Note that at the top of the fetch() block we log the response status value to the console.

const myImage = document.querySelector("img"); const myRequest = new Request("flowers.jpg"); fetch(myRequest).then((response) =>  console.log(response.status); // returns 200 response.blob().then((myBlob) =>  const objectURL = URL.createObjectURL(myBlob); myImage.src = objectURL; >); >); 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Apr 7, 2023 by MDN contributors.

Your blueprint for a better internet.

Источник

pngmark / http-status-codes-javascript

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

var statusMessages = [
‘200’: ‘OK’,
‘201’: ‘Created’,
‘200’: ‘OK’,
‘201’: ‘Created’,
‘202’: ‘Accepted’,
‘203’: ‘Non-Authoritative Information’,
‘204’: ‘No Content’,
‘205’: ‘Reset Content’,
‘206’: ‘Partial Content’,
‘207’: ‘Multi-Status (WebDAV)’,
‘208’: ‘Already Reported (WebDAV)’,
‘226’: ‘IM Used’,
‘300’: ‘Multiple Choices’,
‘301’: ‘Moved Permanently’,
‘302’: ‘Found’,
‘303’: ‘See Other’,
‘304’: ‘Not Modified’,
‘305’: ‘Use Proxy’,
‘306’: ‘(Unused)’,
‘307’: ‘Temporary Redirect’,
‘308’: ‘Permanent Redirect (experimental)’,
‘400’: ‘Bad Request’,
‘401’: ‘Unauthorized’,
‘402’: ‘Payment Required’,
‘403’: ‘Forbidden’,
‘404’: ‘Not Found’,
‘405’: ‘Method Not Allowed’,
‘406’: ‘Not Acceptable’,
‘407’: ‘Proxy Authentication Required’,
‘408’: ‘Request Timeout’,
‘409’: ‘Conflict’,
‘410’: ‘Gone’,
‘411’: ‘Length Required’,
‘412’: ‘Precondition Failed’,
‘413’: ‘Request Entity Too Large’,
‘414’: ‘Request-URI Too Long’,
‘415’: ‘Unsupported Media Type’,
‘416’: ‘Requested Range Not Satisfiable’,
‘417’: ‘Expectation Failed’,
‘418’: ‘I’m a teapot (RFC 2324)’,
‘420’: ‘Enhance Your Calm (Twitter)’,
‘422’: ‘Unprocessable Entity (WebDAV)’,
‘423’: ‘Locked (WebDAV)’,
‘424’: ‘Failed Dependency (WebDAV)’,
‘425’: ‘Reserved for WebDAV’,
‘426’: ‘Upgrade Required’,
‘428’: ‘Precondition Required’,
‘429’: ‘Too Many Requests’,
‘431’: ‘Request Header Fields Too Large’,
‘444’: ‘No Response (Nginx)’,
‘449’: ‘Retry With (Microsoft)’,
‘450’: ‘Blocked by Windows Parental Controls (Microsoft)’,
‘451’: ‘Unavailable For Legal Reasons’,
‘499’: ‘Client Closed Request (Nginx)’,
‘500’: ‘Internal Server Error’,
‘501’: ‘Not Implemented’,
‘502’: ‘Bad Gateway’,
‘503’: ‘Service Unavailable’,
‘504’: ‘Gateway Timeout’,
‘505’: ‘HTTP Version Not Supported’,
‘506’: ‘Variant Also Negotiates (Experimental)’,
‘507’: ‘Insufficient Storage (WebDAV)’,
‘508’: ‘Loop Detected (WebDAV)’,
‘509’: ‘Bandwidth Limit Exceeded (Apache)’,
‘510’: ‘Not Extended’,
‘511’: ‘Network Authentication Required’,
‘598’: ‘Network read timeout error’,
‘599’: ‘Network connect timeout error’, ‘
];

is this an array or an object? 🤔

I’m feeling unexpected token :

There are a few errors with this code indeed. Thankfully, the fix is to just replace the [] with <> , and remove the trailing apostrophe in the second last line. You’ll also have to add a backslash ( \ ) before the unescaped apostrophe in the value of status code 418 to fix another error too. This data was probably scraped off a webpage and that’s why there are these errors. And, for some reason, the two keys/values are duplicates so remove one of the pairs.

I’ve made an object array version tho and added some more codes that I knew.

const possibleHTTPArray = [  status: '200', message: 'OK' >,  status: '201', message: 'Created' >,  status: '202', message: 'Accepted' >,  status: '203', message: 'Non-Authoritative Information' >,  status: '204', message: 'No Content' >,  status: '205', message: 'Reset Content' >,  status: '206', message: 'Partial Content' >,  status: '207', message: 'Multi-Status' >,  status: '208', message: 'Already Reported (WebDAV)' >,  status: '226', message: 'IM Used' >,  status: '300', message: 'Multiple Choices' >,  status: '301', message: 'Moved Permanently' >,  status: '302', message: 'Found' >,  status: '303', message: 'See Other' >,  status: '304', message: 'Not Modified' >,  status: '305', message: 'Use Proxy' >,  status: '307', message: 'Temporary Redirect' >,  status: '308', message: 'Permanent Redirect' >,  status: '400', message: 'Bad Request' >,  status: '401', message: 'Unauthorized' >,  status: '402', message: 'Payment Required' >,  status: '403', message: 'Forbidden' >,  status: '404', message: 'Not Found' >,  status: '405', message: 'Method Not Allowed' >,  status: '406', message: 'Not Acceptable' >,  status: '407', message: 'Proxy Authentication Required' >,  status: '408', message: 'Request Timeout' >,  status: '409', message: 'Conflict' >,  status: '410', message: 'Gone' >,  status: '411', message: 'Length Required' >,  status: '412', message: 'Precondition Failed' >,  status: '413', message: 'Request Entity Too Large' >,  status: '414', message: 'Request-URI Too Long' >,  status: '415', message: 'Unsupported Media Type' >,  status: '416', message: 'Requested Range Not Satisfiable' >,  status: '417', message: 'Expectation Failed' >,  status: '418', message: `I'm a teapot` >,  status: '420', message: 'Enhance Your Calm' >,  status: '421', message: 'Misdirected Request' >,  status: '422', message: 'Unprocessable Entity' >,  status: '423', message: 'Locked' >,  status: '424', message: 'Failed Dependency' >,  status: '425', message: 'Too Early' >,  status: '426', message: 'Upgrade Required' >,  status: '428', message: 'Precondition Required' >,  status: '429', message: 'Too Many Requests' >,  status: '431', message: 'Request Header Fields Too Large' >,  status: '444', message: 'No Response' >,  status: '449', message: 'Retry With' >,  status: '450', message: 'Blocked by Windows Parental Controls' >,  status: '451', message: 'Unavailable For Legal Reasons' >,  status: '497', message: 'HTTP Request Sent to HTTPS Port' >,  status: '498', message: 'Token expired/invalid' >,  status: '499', message: 'Client Closed Request' >,  status: '500', message: 'Internal Server Error' >,  status: '501', message: 'Not Implemented' >,  status: '502', message: 'Bad Gateway' >,  status: '503', message: 'Service Unavailable' >,  status: '504', message: 'Gateway Timeout' >,  status: '505', message: 'HTTP Version Not Supported' >,  status: '506', message: 'Variant Also Negotiates' >,  status: '507', message: 'Insufficient Storage' >,  status: '508', message: 'Loop Detected' >,  status: '509', message: 'Bandwidth Limit Exceeded' >,  status: '510', message: 'Not Extended' >,  status: '511', message: 'Network Authentication Required' >,  status: '521', message: 'Web Server is Down' >,  status: '523', message: 'Origin is Unreachable' >,  status: '525', message: 'SSL Handshake Failed' >,  status: '598', message: 'Network read timeout error' >,  status: '599', message: 'Network connect timeout error' >, ];

Источник

Читайте также:  Java version from code
Оцените статью