Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 884
Tomas Grosup Avatar asked Dec 03 '12 15:12

Tomas Grosup


2 Answers

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"
like image 133
Sampson Avatar answered Sep 30 '22 00:09

Sampson


You can use http-status-codes from npm.

    var HttpStatus = require('http-status-codes');
    HttpStatus.getStatusText(200) // ==> "OK"
like image 25
ibasoni Avatar answered Sep 30 '22 01:09

ibasoni