如何从 422 Unprocessable Entity 响应中获取错误消息
How to fetch the error message from 422 Unprocessable Entity response
我打了一个 api 电话 returns 我这个
Response{protocol=http/1.1, code=422, message=Unprocessable Entity, url=https://someapi/endpoint}
在日志中,我得到以下响应:
{"message":"Validation Failed","errors":{"email":["has already been taken"]}}
我正在开发一个具有个人资料创建功能的 Android 应用程序,我想将用户重定向回来,以便它可以在我收到此回复时更改其电子邮件地址,但为此我需要获取并处理 "errors" 消息。
如何从错误正文中获取消息?我试过这样:
response.message()
但我只得到
Unprocessable Entity
像下面这样尝试
.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();
}
});
希望对您有所帮助:-)
我打了一个 api 电话 returns 我这个
Response{protocol=http/1.1, code=422, message=Unprocessable Entity, url=https://someapi/endpoint}
在日志中,我得到以下响应:
{"message":"Validation Failed","errors":{"email":["has already been taken"]}}
我正在开发一个具有个人资料创建功能的 Android 应用程序,我想将用户重定向回来,以便它可以在我收到此回复时更改其电子邮件地址,但为此我需要获取并处理 "errors" 消息。
如何从错误正文中获取消息?我试过这样:
response.message()
但我只得到
Unprocessable Entity
像下面这样尝试
.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();
}
});
希望对您有所帮助:-)