Sandak Sandak - 1 year ago 174
reST (reStructuredText) Question

Android Volley POST string in body

I'm trying to use Volley library to communicate with my RESTful API.

I have to POST string in the body, when I'm asking for the bearer Token. String should look like this:
And header:
Content-Type: application/x-www-form-urlencoded

More info about WebApi Individual Accounts:

Unfortunately I can't figure out how can I solve it..

I'm trying something like this:

StringRequest req = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
public void onResponse(String response) {
VolleyLog.v("Response:%n %s", response);
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("username", "User0");
params.put("password", "Password0");
return params;

public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;

I'm getting 400 Bad Request all the time.
I think that I'm actually sending request like this:

grant_type:password, username:User0, password:Password0

instead of:


I would be very grateful if anyone has any ideas or an advice..

Answer Source

First thing, I advise you to see exactly what you're sending by either printing to the log or using a network sniffer like wireshark or fiddler.

How about trying to put the params in the body? If you still want a StringRequest you'll need to extend it and override the getBody() method (similarly to JsonObjectRequest)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download