Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch the error message from 422 Unprocessable Entity response

I make an api call which returns me this

Response{protocol=http/1.1, code=422, message=Unprocessable Entity, url=https://someapi/endpoint}

In the logs, along with the response i get the following:

{"message":"Validation Failed","errors":{"email":["has already been taken"]}}

I'm working on an Android app that has a profile creation feature and I want to redirect the user back so it can change its email address when I receive this response but for that I need to fetch and handle the "errors" message.

How can i get the message from the error body ? I've tried like this:

response.message()

But I get only

Unprocessable Entity

like image 491
petryk33 Avatar asked Feb 19 '18 11:02

petryk33


People also ask

How do I get a 422 error?

422 Unprocessable Entity The request body was well-formed but contains semantical errors. The response body will provide more details in the errors or error parameters. Sending invalid or unknown data results in a 422 Unprocessable Entity response. The common reason for error 422 is the wrong character within the code.

What is Unprocessable entity error?

The HyperText Transfer Protocol (HTTP) 422 Unprocessable Entity response status code indicates that the server understands the content type of the request entity, and the syntax of the request entity is correct, but it was unable to process the contained instructions.

What is error code 422 in strike?

The full name of the error code is 422 “unprocessable entity.” See how Kinsta stacks up against the competition. In a nutshell, the error means that you're making a request the server understands, but it can't process it.

What is the status code for update?

For a PUT request: HTTP 200, HTTP 204 should imply "resource updated successfully". HTTP 201 if the PUT request created a new resource. For a DELETE request: HTTP 200 or HTTP 204 should imply "resource deleted successfully".


1 Answers

Try like below

 .subscribe(res-> {
                      //success case

                    },
                    t -> {
                        if (t instanceof HttpException) {
                           if (((HttpException) t).code() == 422) {
                           String errorResponse=((HttpException) t).response().errorBody().string();
                         //your validations
                        }
                        } else {

                            t.printStackTrace();

                        }
                    });

I hope it's will help you :-)

like image 86
Rajasekaran M Avatar answered Sep 28 '22 09:09

Rajasekaran M