Get HTTP status code from a response coming as empty with $http service - angularjs

I'm having trouble getting the HTTP status code from a empty response that is empty. status code is required to show the corresponding message that happened.
I Tried everything but couldn't get the http status code from the response. Does anyone have an idea ?

I believe this server is broken. I don't think there's a circumstance where POST can result in a 304 Not Modified response.
Usually 304 Not Modified is in response to a GET or HEAD request, when If-Match or If-Modified-Since is used.
However, if those headers are used for other HTTP requests (such as POST), the correct response is 412 Precondition Failed.
You usually never see a 304, because when it's used correctly (with GET), the browser will still not expose the 304, but instead it will give you a 200 and a response that's served directly from cache.
So in short, my best guess is that you can't, because your server doesn't conform with HTTP all assumptions are kind of out of the window.

A Promise that will be resolved (request success) or rejected (request failure) with a response object.
The response object has these properties:
data – {string|Object} – The response body transformed with the transform functions.
status – {number} – HTTP status code of the response.
headers – {function([headerName])} – Header getter function.
config – {Object} – The configuration object that was used to generate the request.
statusText – {string} – HTTP status text of the response.
xhrStatus – {string} – Status of the XMLHttpRequest (complete, error, timeout or abort).
So in your case you can add second parameter as status to get status code

Related

Restangular CORS error is failing with status code of -1

The restangular docs states that the API with CORS error fails with a status code of 0, but mine is failing with -1.
From the docs:
This is typically caused by Cross Origin Request policy. In order to enable cross domain communication and get correct response with appropriate status codes, you must have the CORS headers attached, even in error responses. If the server does not attach the CORS headers to the response then the XHR object won't parse it, thus the XHR object won't have any response body, status or any other response data inside which typically will cause your request to fail with status code 0.
I have looked into similar questions on SO, but couldn't find anything related to this.

HTTP 404 vs 400 for invalid query parameters

Here is my request URL:
http://server.com/app/user/getuser/?userId=9999
Note that userId is query parameter. Not embedded path parameter.
I understand that if the request URL is: http://server.com/app/user/getuser/9999 and the ID 9999 does not exist in database, The code 404 should be used.
BUT what HTTP status should be used for the case userId is query parameter? Right now I am returning 400 instead of 404.
I would use 404 Not Found.
Why?
The RFC 7231 defines a 400 Bad Request response like this:
The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
...since your request is valid and you are just trying to access a resource that does not exist, I think a 404 Not Found status is more suitable. RFC 7231 defines its meaning like this:
The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
In my opinion, if the "user" is the resource, a better way to expose that resource is to use a URL like this: http://server.com/app/user/9999
Query parameters are best for searching a dataset, where an empty answer is still valid (200).
But, if you can't change the URL, 404 in this case is a valid option.

Whenever a CORS $http request fails, the response returned is always 0

this is a similar question to this post in SO.But the answer provided here cannot be applied in my case as i cannot change the response headers from server.
Suppose an http POST to a different origin. This implies CORS, including a CORS preflight exchange. Now suppose the OPTIONS request returns a 503 service unavailable error due to a server problem. In this case the error handler gives ""for data and 0 for status instead of giving me the status code 503 and the text:service not available. An example of this scenario is illustrated in the below image.
I am using angularjs $http and i know in the response there are no CORS header if such errors happen.and i cannot change it.
Is there any way i can receive the proper error code and the text in my rejection object.
This is not an issue of AngularJS / $http but it is the behavior of the browsers and their XMLHttpRequest object: If the CORS request fails, the browser does not give any information back to the caller.
Before I got this understanding, I also though it to be an AngularJS issue and I raised an open issue on github of Angular -> with the corresponding comment.
https://github.com/angular/angular.js/issues/13085#issuecomment-148047721
So I think there seems no other way to solve this as to add the Access-Control-Allow-Origin response header also on the proxy / load balancer in case of 503.
Edit:
If your load balancer is a HAproxy, the following may help you too:
HAproxy: different 503 errorfile for OPTIONS and POST methods
It shows how to let HAproxy anwer the CORS requests autonomous.

How to customize ServletResponse in Apache Solr?

Current behavior:
No matter what the response is, as long as we can get valid reponse (for example, JSON) from Solr, the http status code is always 200.
Expected behavior:
Within JSON response, we currently have other status codes such as 500(Internal Server Error) to be returned, and we need to change the response status code into 500 in http response header.
I know we can parse the response JSON, grab the status code and set it as http response code in a specific filter in filter chain, it may not be expensive, but obviously it takes time.
I am wondering if there is a way to overwrite the http status code when generating the JSON response in solr, so we don't need to do extra work in filter chain? I need it for both Solr 1.4 and Solr 4.0
Thanks for any help.

how to send a post error in angularjs

Is there a way to send an error back to JS when we do a $http.post?
Right now if there is an error server side, I return a response tagged as "error" but it's captured in success on the Angularjs side.
It would be better if I send directly to error instead of having a condition in the success of Angular.
Is that possible?
According to the AngularJS documentation for $http:
A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the error callback will not be called for such responses.
If you want your HTTP call to result in the error handler being called you need to return a non 2XX response code (and also non-redirect as mentioned). You can find a pretty full list of HTTP response codes here

Resources