So when I make a POST API call to my server, I get a 400 Bad Request error with JSON response.
{ "userMessage": "Blah", "internalMessage": "Bad Request blah blah", "errorCode": 1 }
I call it by
Call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { //AA } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { //BB } }
However the problem is that once I get the response, onFailure() is invoke so that //BB is called. Here, I have no way to access the JSON response. When I log the api request and response, it doesn't show JSON response at all. And Throwable t is IOException. However, strangely, when I make the same call on Postman, it does return the expected JSON response with 400 error code.
So my question is how can I get the json response when I get 400 Bad Request error? Should I add something to okhttpclient?
Thanks
you can simply use "response. message(). toString()" which will give the same error string in a more readable format.
The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).
You can do it in your onResponse
method, remember 400 is a response status not an error:
if (response.code() == 400) { Log.v("Error code 400",response.errorBody().string()); }
And you can handle any response code except 200-300 with Gson
like that:
if (response.code() == 400) { Gson gson = new GsonBuilder().create(); ErrorPojoClass mError=new ErrorPojoClass(); try { mError= gson.fromJson(response.errorBody().string(),ErrorPojoClass.class); Toast.makeText(context, mError.getDescription(), Toast.LENGTH_LONG).show(); } catch (IOException e) { // handle failure to read error } }
Add this to your build.gradle
: compile 'com.google.code.gson:gson:2.7'
If you want create Pojo
class go to Json Schema 2 Pojo and paste your example Json
response. Select source type Json and annotation Gson .
You can try the below code to get 400 response. You can get error response from errorBody() method.
Call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { //get success and error response here if (response.code() == 400) { if(!response.isSuccessful()) { JSONObject jsonObject = null; try { jsonObject = new JSONObject(response.errorBody().string()); String userMessage = jsonObject.getString("userMessage"); String internalMessage = jsonObject.getString("internalMessage"); String errorCode = jsonObject.getString("errorCode"); } catch (JSONException e) { e.printStackTrace(); } } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { //get failure response here } } }
EDIT: Fixed method name from toString
to string
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