2

In my Android application, I am using google's volley for network operations. There is a case where I need to make a request but send body as form-data. I have tried everything else, but I am not able to make the request as form-data.

Here is a curl

curl -X POST -H "Content-Type: multipart/form-data" -F "mobile_number=<XXXX>" "<server_url>"

How can I achieve that -F part in volley? The Server is throwing bad request.

This is what I have done :

final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, URLFactory.OTP_URL,
                null, listener, errorListener){

            @Override
            public byte[] getBody() {
                final JSONObject  jsonObject = new JSONObject();
                try {
                    jsonObject.put("mobile_number", mobileNumber);
                } catch (JSONException e) {
                    e.printStackTrace();
                    return null;
                }
                return jsonObject.toString().getBytes();
            }


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                final HashMap<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "multipart/form-data");
                return headers;
            }
        };

Please help me in this.

Kaustubh
  • 653
  • 6
  • 21
  • Have you read http://stackoverflow.com/questions/32240177/working-post-multipart-request-with-volley-and-without-httpentity?lq=1 yet? – BNK Jun 13 '16 at 06:16

1 Answers1

2

This can be done in volley by creating the postBody yourself. Please refer the below code.

Code for creating the body:

private String createPostBody(Map<String, String> params) {
        StringBuilder sbPost = new StringBuilder();
         for (String key : params.keySet()) {
                if (params.get(key) != null) {
                    sbPost.append("\r\n" + "--" + BOUNDARY + "\r\n");
                    sbPost.append("Content-Disposition: form-data; name=\"" + key + "\"" + "\r\n\r\n");
                    sbPost.append(params.get(key));
                }
            }

        return sbPost.toString();
    }

Modifed getBody() code :

  @Override
            public byte[] getBody() {
                Map<String,String> params = new HashMap<>();
                params.add("mobile_number", mobileNumber);
                String postBody = createPostBody(params);

                return postBody.getBytes();
            }

You will need to modify getHeaders as well to tell the server what you boundary is :

 @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                final HashMap<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "multipart/form-data;boundary=" + BOUNDARY;);
                return headers;
            }
Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
  • what is a boundary? sbPost.append("\r\n" + "--" + BOUNDARY + "\r\n"); – Kaustubh Jun 13 '16 at 07:13
  • This is string which server uses to split the the different parameters received in the post body. You can refer the below link : http://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data – Arpit Ratan Jun 13 '16 at 07:15
  • 1
    I have added getHeader() function as well. You can define any string as boundary. For eg. private static final String BOUNDARY = "s2retfgsGSRFsERFGHfg"; – Arpit Ratan Jun 13 '16 at 07:21