I\'m trying to make a POST
request to my API and it works in Postman
(I get a valid JSON object), but not using Volley
. With the follo
I have removed this params.put("Content-Type", "application/x-www-form-urlencoded");
This is my Code Change.
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
if(!isLogout) {
params.put("username", username);
params.put("password", password);
params.put("grant_type", "password");
} else {
}
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
if(isLogout) {
params.put("Authorization", "bearer "+LibraryDataModel.getToken());
}else {
// Removed this line
// params.put("Content-Type", "application/x-www-form-urlencoded");
}
return params;
}
In Android 2.3 there is a problem using Base64.encodeToString() as it introduces a new line in HTTP header. See my response to this question here in SO.
Short answer: Don't use Base64.encodeToString() but put the already encoded String there.
400 indicates a bad request, maybe you're missing Content-Type=application/json
on your headers
Somertimes this error 400 is arise due to Request type so you need to change the Request.Method.GET
to Request.Method.POST
and than it works like a charm.
I have find a solution if you are using postman for hitting the api and your api have defined serializer field like skill = serializers.JSONField() then that type of error occurred-
Solution For POSTMAN- you just add binary=True inside JSONField ex- skill = serializers.JSONField(binary=True)
Solution for Android or other client then- you just remove binary=True inside JSONField ex- skill = serializers.JSONField()
I've got this error and now Iit's been fixed. I did what is told in this link. What you need to do is
if (statusCode < 200 || statusCode > 299) {
throw new IOException();
}
with
if (statusCode < 200 || statusCode > 405) {
throw new IOException();
}
Hope this helps.