Are you too getting 400 Bad Request error when using volley? This is the problem many developers face when using volley. And they are not sure of the reason as everything in request seems okay to them. But they still could not find the reason behind this error. And sometimes, the same code works on one server and gives error on some other server. So, Lets discuss here that why Volley gives this 400 Bad Request error and how we can resolve it.
Why Android Volley gives 400 Bad Request error?
Volley is a powerful library, so if you get the bad request error you should understand that actually there is some problem with the Request object because of which you are getting the error. When you make a request using volley there are some headers that volley automatically adds to your Request Object. For example, when you make a json request, volley sets
"application/json" . However this header in not added when you are extending plain Request Object. So, we have to understand that we may not be setting the right headers when we are making the request that is why it gives us 400 Bad Request error.
But sometimes in some setups even when making JsonRequest and headers being added automatically, you will notice that content type header is not updating and hence you get a bad request error. Volley does not treat the
Content-Type header in the same way as it treats other header. Particularly, overriding the
getHeaders() method to define the content type does not always work.
How to resolve 400 Bad Request eror while using Android Volley?
While making request if the
Content-Type header does not reflect and your request generated 400 bad request header, then you may need to override the
getBodyContentType() method in order to get the
Content-Type header to update correctly. Following code shows how to override the
getBodyContentType() method in your Request class, to update the content type to json:
public String getBodyContentType()
return "application/json; charset=utf-8";
Similarly you can return various string types depending on your request. For example, for a multipart request you may return
"multipart/form-data; charset=utf-8" and for xml request you may return
"application/xml" in the
getBodyContentType() method. Note, setting the
Content-Type header and overriding the
getBodyContentType() method becomes necessary when you are using a PUT request.
Anotherthing you need to be careful about while setting the body content type is that your settings of the content type should match with what server is expecting. If even after overriding body content type the problem still exist then you need to check if you are setting the right charset and boundary in content type.
For example, for some developers setting
"application/json" may be enough but for others depending upon how the server is expecting the request, they may need to define charset along with content type. Therefore, using “application/json; charset=utf-8″ might help.
If the above solution does not work for you, then you may refer following links to deep drill the issue: