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


when use postman, getting correct response (server processes "categories" array) server using request description below: postman request body

with header: postman request headers

this request has raw representation:

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 how represent described request fire correctly using volley library. have tried many variants, 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 of them either don't encode "categories" array (so server doesn't take variable in account @ all) or make encoding in incorrect way, server can't obtain values of array.

update:

i've tried fire request using okhttp , absolutely correct (as in case 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(); 

that means 1 thing: volley processes raw request bodies in other way (maybe incorrect one) or there subtle config in volley missing perform such requests correctly.

i should admit problem described in question based on server's logic. dumping request bodies volley , okhttp , comparing them step step find out have difference in 1 header - content-type. in okhttp like:

content-type: application/x-www-form-urlencoded; charset=utf-8 

while in volley was:

content-type: application/x-www-form-urlencoded, application/x-www-form-urlencoded; charset=utf-8 

so looks kind of strict check of headers on server. make volley's headers equal okhttp's, had remove overriding of getheaders() method in request , return body type description in getbodycontenttype() method:

@override public string getbodycontenttype() {     return "application/x-www-form-urlencoded"; } 

i can't question solved, because there still can more answers how add arrays in post-request bodies using more elegant ways (without raw bodies , other formats - example json).


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -