How can I convert numeric HTTP status code to its display name in javascript?
Asked Answered
D

5

9

Is there a way of converting numeric http status codes (i.e. 404,403,500,...) to their display names (Not Found, Forbidden, Internal Server Error,...) ?

If it helps anything, I am using AngularJS and jQuery.

Durham answered 3/12, 2012 at 15:33 Comment(1)
If nothing else, there's a reliable list you could grab from Wikipedia. It could be used to create an array/object mapper. en.wikipedia.org/wiki/List_of_HTTP_status_codesParthenos
J
5

You could construct your own object (or download this one) and look them up there:

var codes = {
    "100": "Continue",
    "101": "Switching Protocols",
    "102": "Processing",
    "200": "OK",
    ...
};

var code = 200;

console.log(codes[code]); // "Ok"
Justifier answered 3/12, 2012 at 15:40 Comment(1)
Thanks, this is much betterDurham
R
16

Just to help everyone to copy/paste

/* Helpers */
var friendlyHttpStatus = {
    '200': 'OK',
    '201': 'Created',
    '202': 'Accepted',
    '203': 'Non-Authoritative Information',
    '204': 'No Content',
    '205': 'Reset Content',
    '206': 'Partial Content',
    '300': 'Multiple Choices',
    '301': 'Moved Permanently',
    '302': 'Found',
    '303': 'See Other',
    '304': 'Not Modified',
    '305': 'Use Proxy',
    '306': 'Unused',
    '307': 'Temporary Redirect',
    '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 Required',
    '413': 'Request Entry Too Large',
    '414': 'Request-URI Too Long',
    '415': 'Unsupported Media Type',
    '416': 'Requested Range Not Satisfiable',
    '417': 'Expectation Failed',
    '418': 'I\'m a teapot',
    '429': 'Too Many Requests',
    '500': 'Internal Server Error',
    '501': 'Not Implemented',
    '502': 'Bad Gateway',
    '503': 'Service Unavailable',
    '504': 'Gateway Timeout',
    '505': 'HTTP Version Not Supported',
};

/* Usage */
var code = 200;

console.log(friendlyHttpStatus[code]); // "Ok"
Rosenzweig answered 10/1, 2017 at 12:13 Comment(0)
J
5

You could construct your own object (or download this one) and look them up there:

var codes = {
    "100": "Continue",
    "101": "Switching Protocols",
    "102": "Processing",
    "200": "OK",
    ...
};

var code = 200;

console.log(codes[code]); // "Ok"
Justifier answered 3/12, 2012 at 15:40 Comment(1)
Thanks, this is much betterDurham
I
4

You can use http-status-codes from npm.

    var HttpStatus = require('http-status-codes');
    HttpStatus.getStatusText(200) // ==> "OK"
Indiscretion answered 11/3, 2020 at 15:46 Comment(0)
S
1

I don't think so, but you can use this useful site : http://httpstat.us/ .

Survival answered 3/12, 2012 at 15:36 Comment(2)
Still my last option, but this page would be at least easier to parseDurham
Yes, I have created some sample code : jsfiddle.net/scaillerie/FWp5b . But for some statuses it is impossible (for example, 301...).Survival
J
0

My crappy solution:

var friendlyHttpStatus = function(status){

    if(status == '200') return 'OK'
    if(status == '201') return 'Created'
    if(status == '202') return 'Accepted'
    if(status == '203') return 'Non-Authoritative Information'
    if(status == '204') return 'No Content'
    if(status == '205') return 'Reset Content'
    if(status == '206') return 'Partial Content'
    if(status == '300') return 'Multiple Choices'
    if(status == '301') return 'Moved Permanently'
    if(status == '302') return 'Found'
    if(status == '303') return 'See Other'
    if(status == '304') return 'Not Modified'
    if(status == '305') return 'Use Proxy'
    if(status == '306') return 'Unused'
    if(status == '307') return 'Temporary Redirect'
    if(status == '400') return 'Bad Request'
    if(status == '401') return 'Unauthorized'
    if(status == '402') return 'Payment Required'
    if(status == '403') return 'Forbidden'
    if(status == '404') return 'Not Found'
    if(status == '405') return 'Method Not Allowed'
    if(status == '406') return 'Not Acceptable'
    if(status == '407') return 'Proxy Authentication Required'
    if(status == '408') return 'Request Timeout'
    if(status == '409') return 'Conflict'
    if(status == '410') return 'Gone'
    if(status == '411') return 'Length Required'
    if(status == '412') return 'Precondition Required'
    if(status == '413') return 'Request Entry Too Large'
    if(status == '414') return 'Request-URI Too Long'
    if(status == '415') return 'Unsupported Media Type'
    if(status == '416') return 'Requested Range Not Satisfiable'
    if(status == '417') return 'Expectation Failed'
    if(status == '418') return 'I\'m a teapot'
    if(status == '500') return 'Internal Server Error'
    if(status == '501') return 'Not Implemented'
    if(status == '502') return 'Bad Gateway'
    if(status == '503') return 'Service Unavailable'
    if(status == '504') return 'Gateway Timeout'
    if(status == '505') return 'HTTP Version Not Supported'
}
Jarvisjary answered 18/3, 2014 at 18:39 Comment(1)
i hate tons of if...Mcmurry

© 2022 - 2024 — McMap. All rights reserved.