What is the correct way to send an array in a POST request using Volley?

When I use Postman, I get the correct response (server processes "categories as array") from the server using the request description below: Postman request body

with the title: postman request headers

This request has this raw view:

POST /api/items HTTP/1.1
Host: www1.
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: c18f92ee-2f36-f4dd-2963-6bc6d1db0bdf

items=247642&categories%5B%5D=12&categories%5B%5D=11

      

My question is how to represent the described request in order to run it correctly using the Volley library. I've tried many options, for example:

@Override
public byte[] getBody() throws AuthFailureError {
    return "items=247642&categories%5B%5D=12".getBytes();
}

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

      

but they all either do not encode "categories" as an array (so the server does not accept this variable in the account at all), or they do not encode the encoding correctly, so the server cannot get the values โ€‹โ€‹of this array.

UPDATE:

I just tried to run this request with OkHttp and it was absolutely correct (as with Postman above):

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
            RequestBody body = RequestBody.create(mediaType, "items=247642&categories%5B%5D=10&categories%5B%5D=9");
            Request request = new Request.Builder()
                    .url("http://www1...")
                    .post(body)
                    .addHeader("content-type", "application/x-www-form-urlencoded")
                    .addHeader("cache-control", "no-cache")
                    .build();

      

This only means one thing: Volley is handling raw request bodies in some other way (possibly wrong), or there is some subtle configuration in Volley missing to properly execute such requests.

+3


source to share


1 answer


I must admit that the problem described in the question is mostly based on server logic. Dropping Volley and OkHttp body requests and comparing them step by step, I can find out that they only have a difference in one header - Content-Type

. In OkHttp it looked like this:

Content-Type: application/x-www-form-urlencoded; charset=utf-8

      

while in Volley it was:

Content-Type: application/x-www-form-urlencoded, application/x-www-form-urlencoded; charset=UTF-8

      



It looks like it was some kind of strict headers check on the server. To make the Volley headers equal OkHttp, I had to remove the method override getHeaders()

in the request and return the body type description in the method getBodyContentType()

:

@Override
public String getBodyContentType() {
    return "application/x-www-form-urlencoded";
}

      

I can't say my question is completely resolved because there might still be more answers on how to add arrays in POST request bodies using more elegant ways (no raw bodies and other formats - like JSON).

0


source







All Articles