Android Volley POST string in body

To send a normal POST request (no JSON) with parameters like username and password, you’d usually override getParams() and pass a Map of parameters:

public void HttpPOSTRequestWithParameters() {
    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "http://www.somewebsite.com/login.asp";
    StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
        new Response.Listener<String>() 
        {
            @Override
            public void onResponse(String response) {
                Log.d("Response", response);
            }
        }, 
        new Response.ErrorListener() 
        {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("ERROR","error => "+error.toString());
            }
        }
            ) {     
        // this is the relevant method
        @Override
        protected Map<String, String> getParams() 
        {  
            Map<String, String>  params = new HashMap<String, String>();
            params.put("grant_type", "password"); 
            // volley will escape this for you 
            params.put("randomFieldFilledWithAwkwardCharacters", "{{%stuffToBe Escaped/");
            params.put("username", "Alice");  
            params.put("password", "password123");

            return params;
        }
    };
    queue.add(postRequest);
}

And to send an arbitary string as POST body data in a Volley StringRequest, you override getBody()

public void HttpPOSTRequestWithArbitaryStringBody() {
    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "http://www.somewebsite.com/login.asp";
    StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
        new Response.Listener<String>() 
        {
            @Override
            public void onResponse(String response) {
                Log.d("Response", response);
            }
        }, 
        new Response.ErrorListener() 
        {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("ERROR","error => "+error.toString());
            }
        }
            ) {  
         // this is the relevant method   
        @Override
        public byte[] getBody() throws AuthFailureError {
            String httpPostBody="grant_type=password&username=Alice&password=password123";
            // usually you'd have a field with some values you'd want to escape, you need to do it yourself if overriding getBody. here's how you do it 
            try {
                httpPostBody=httpPostBody+"&randomFieldFilledWithAwkwardCharacters="+URLEncoder.encode("{{%stuffToBe Escaped/","UTF-8");
            } catch (UnsupportedEncodingException exception) {
                Log.e("ERROR", "exception", exception);
                // return null and don't pass any POST string if you encounter encoding error
                return null;
            }
            return httpPostBody.getBytes();
        }
    };
    queue.add(postRequest);
}

As an aside, Volley documentation is non-existent and quality of StackOverflow answers is pretty bad. Can’t believe an answer with an example like this wasn’t here already.

Leave a Comment