1

Here is a simple http request that I make to my Django Rest Framework based API through Postman. enter image description here

I send 4 simple parameters name, phone, password1 and password2 in form of form-data and expect the server to return a json object containing a key named key whose value is a JWT (JSON web token). This is working fine Postman, but whenever I try to do the same thing in my App through volley, I get 400 bad request error. I've tried doing multiple things but all in vain.

Can someone explain to me how to simply post some data in form of form-data in volley?

Here is my code.

public void signUp(View View) throws Exception
   {
       queue = Volley.newRequestQueue(this);
       url = "http://<example>.com/auth/registration/";
       Map<String,String> params = new HashMap<String, String>();
       params.put("name","habc");
       params.put("phone","0324423");
       params.put("password1","pass");
       params.put("password2","pass");
       JsonObjectRequest add_user_request = new JsonObjectRequest(Request.Method.POST, url,new JSONObject(params),
               new Response.Listener<JSONObject>()
               {
                   @Override
                   public void onResponse(JSONObject response) {

                       try {
                           String key = response.getString("key");
                           tv.setText(key);
                       } catch (JSONException e) {
                           // Handle exception 
                       }
                   }
               },
               new Response.ErrorListener()
               {
                   @Override
                   public void onErrorResponse(VolleyError error) {
                       // error.getStackTrace() doesnt print enough information
                   }
               }
       )
       {
           @Override
           public Map<String, String> getHeaders() throws AuthFailureError {
               Map<String,String> params = new HashMap<String, String>();
               params.put("Content-Type","application/x-www-form-urlencoded");
               return params;
           }
       };
       Toast.makeText(this,"Adding requst to queue . . ",Toast.LENGTH_SHORT).show();
       queue.add(add_user_request);
       queue.start();
   }

1 Answer 1

0

You have to add getParams() method ( after the getHeaders() method ) send the data to server.

{
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String,String> params = new HashMap<String, String>();
        params.put("name","habc");
        params.put("phone","0324423");
        params.put("password1","pass");
        params.put("password2","pass");
        return params;
    }
}
4
  • I still get the 400 Server Error. The rest API sends back a json object which contains the details about the error but I am not being able to retrieve that json object from the VolleyError object that I get in Response.ErrorListner. Can you please help me get that error. Jul 3, 2016 at 17:23
  • remove the getHeaders() and try again . Jul 3, 2016 at 17:27
  • I still receive the same error. I need to know how to print the response that the server sends even in case of an error. Jul 3, 2016 at 17:35
  • 1
    In this case you have to send a StringRequest so that you can receive a plain String response . Jul 3, 2016 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.