What’s the appropriate HTTP status code to return if a user tries logging in with an incorrect username / password, but correct format?

If you are strictly using the HTTP authentication framework provided by RFC 7235 for your REST API, the correct HTTP code would actually be 401. From the RFC: The 401 (Unauthorized) status code indicates that the request has not been applied because it lacks valid authentication credentials for the target resource. The server generating a … Read more

How to get HTTP response code for a URL in Java?

HttpURLConnection: URL url = new URL(“http://example.com”); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod(“GET”); connection.connect(); int code = connection.getResponseCode(); This is by no means a robust example; you’ll need to handle IOExceptions and whatnot. But it should get you started. If you need something with more capability, check out HttpClient.

Difference between HTTP redirect codes

301: Permanent redirect. Clients making subsequent requests for this resource should use the new URI. Clients should not follow the redirect automatically for POST/PUT/DELETE requests. 302: Redirect for undefined reason. Clients making subsequent requests for this resource should not use the new URI. Clients should not follow the redirect automatically for POST/PUT/DELETE requests. 303: Redirect … Read more

What is correct HTTP status code when redirecting to a login page?

I’d say 303 see other 302 Found: The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field. fits a login page most closely … Read more

System.Net.WebException HTTP status code

Maybe something like this… try { // … } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { var response = ex.Response as HttpWebResponse; if (response != null) { Console.WriteLine(“HTTP Status Code: ” + (int)response.StatusCode); } else { // no http status code available } } else { // no http status code available } … Read more

Throw HttpResponseException or return Request.CreateErrorResponse?

The approach I have taken is to just throw exceptions from the api controller actions and have an exception filter registered that processes the exception and sets an appropriate response on the action execution context. The filter exposes a fluent interface that provides a means of registering handlers for specific types of exceptions prior to … Read more

Which HTTP status code means “Not Ready Yet, Try Again Later”? [closed]

The “problem”, such as it is, is on the server side: the client has made a well formed request, but the server can not satisfy it. So I’m inclined to a “Server Error”, 5xx status code. Quoth RFC 7231 (the current HTTP standard, emphasis added): The 5xx (Server Error) class of status code indicates that … Read more