I am working on a web service. I want to return the 401: Unauthorized
response to the user for invalid credentials.
How do I manually return this response code?
For error status codes like 401, use the more specific sendError(): httpResponse. sendError(HttpServletResponse. SC_UNAUTHORIZED, "your message goes here");
The HyperText Transfer Protocol (HTTP) 401 Unauthorized response status code indicates that the client request has not been completed because it lacks valid authentication credentials for the requested resource.
If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. 403 Forbidden: The server understood the request, but is refusing to fulfill it. From your use case, it appears that the user is not authenticated.
assuming you are using servlets, you would set the http status to 401 using the setStatus
method:
httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
HttpServletResponse info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With