Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get HTTP status code name using JS and AngularJS?

I have an AngularJS, JS, JQ, HTML5, CSS3 web app. It is suposed to send different HTTP methods to REST API of our projects and manipulate them. It has similar behavior to DHC by Restlet (formerly Dev HTTP Client). Every request returns a status code like 200, 201, 404, 500 etc. which is then displayed to the user.

Now what I would like is to display not only a response code, but also a description of it like this:

404 Not Found, 201 Created etc.

I am getting a response from Angular like this:

$http(config).success(function (data, status, headers, config) {//some logic}

Does anyone know if this is possible using AngularJS?

like image 278
amenoire Avatar asked Apr 10 '14 11:04

amenoire


1 Answers

Well, I ended up with the following solution:

I created a constant variable and listed all HTTP codes and their description (you can copy them to your own program):

constants.js:

var HTTP_STATUS_CODES = {
        'CODE_200' : 'OK',
        'CODE_201' : 'Created',
        'CODE_202' : 'Accepted',
        'CODE_203' : 'Non-Authoritative Information',
        'CODE_204' : 'No Content',
        'CODE_205' : 'Reset Content',
        'CODE_206' : 'Partial Content',
        'CODE_300' : 'Multiple Choices',
        'CODE_301' : 'Moved Permanently',
        'CODE_302' : 'Found',
        'CODE_303' : 'See Other',
        'CODE_304' : 'Not Modified',
        'CODE_305' : 'Use Proxy',
        'CODE_307' : 'Temporary Redirect',
        'CODE_400' : 'Bad Request',
        'CODE_401' : 'Unauthorized',
        'CODE_402' : 'Payment Required',
        'CODE_403' : 'Forbidden',
        'CODE_404' : 'Not Found',
        'CODE_405' : 'Method Not Allowed',
        'CODE_406' : 'Not Acceptable',
        'CODE_407' : 'Proxy Authentication Required',
        'CODE_408' : 'Request Timeout',
        'CODE_409' : 'Conflict',
        'CODE_410' : 'Gone',
        'CODE_411' : 'Length Required',
        'CODE_412' : 'Precondition Failed',
        'CODE_413' : 'Request Entity Too Large',
        'CODE_414' : 'Request-URI Too Long',
        'CODE_415' : 'Unsupported Media Type',
        'CODE_416' : 'Requested Range Not Satisfiable',
        'CODE_417' : 'Expectation Failed',
        'CODE_500' : 'Internal Server Error',
        'CODE_501' : 'Not Implemented',
        'CODE_502' : 'Bad Gateway',
        'CODE_503' : 'Service Unavailable',
        'CODE_504' : 'Gateway Timeout',
        'CODE_505' : 'HTTP Version Not Supported'
    };

And then in my AngularJS controller, when I receive a status from $http I just call this function, which set's the value of status code message to a model:

setResponseCodeDescr = function(responseCode) {

var responseCodeConstant = 'CODE_'.concat(responseCode);

if (HTTP_STATUS_CODES[responseCodeConstant]){
    $rootScope.response.description = HTTP_STATUS_CODES[responseCodeConstant];
} else {
    $rootScope.response.description = "";
}

}

That's all :)

like image 101
amenoire Avatar answered Oct 10 '22 06:10

amenoire