android - How to POST a Dictionary using @PartMap -
i'm sending post message using retrofit2 upload file plus couple of parameters. of parameters dictionaries. in order this, have following:
@multipart @post("incidents") call<machinerequest> sendmachinerequest(@partmap map<string, requestbody> partmap, @part multipartbody.part image;
the problem is: seems if can upload strings value parameters only. utilize requestbody create(mediatype contenttype, string content)
create entries @partmap map. of course, dictionaries have strings fit in requestbody. leads wrong interpretation in rails backend. values interpreted string , not dictionary.
"incident"=>"{\"type\":\"machinerequest\"}"
instead of
"incident"=>{\"type\":\"machinerequest\"}
so question is: how can add dictionary in @partmap, or how can create reqeustbody holds dictionary?
even though question downvoted, still believe findings can helpful new retrofit , tries post file along more complex java object.
so @partmap map<string, requestbody> partmap
contains keys of string 1 value of string. further added key of string value of hashmap.
map<string,requestbody> mrm = new hashmap<string, requestbody>(); hashmap<string,string> incident = new hashmap<string, string>(); incident.put("type","machinerequest"); requestbody incident = createpartfromstring(string.valueof(incident); requestbody machine = createpartfromstring(string.valueof(getmachine())); mrm.put("incident",incident); mrm.put("machine",machine);
this ended in rails backend "incident"=>"{\"type\":\"machinerequest\"}"
. value of incident
got interpreted string instead of dictionary -> nomethoderror - undefined method permit "{'type':'machinerequest'}":string:
in order fixed changed code follows:
map<string,requestbody> mrm = new hashmap<string, requestbody>(); requestbody incident = createpartfromstring("machinerequest"); requestbody machine = createpartfromstring(string.valueof(getmachine())); mrm.put("incident[type]",incident); mrm.put("machine",machine);
which made arrive expected: "incident"=>{"type"=>"machinerequest"}
, made rails able hold of value of type
.
in end, mrm.put("incident[type]",incident);
made whole difference. @ least @multipart
post requests. non @mutipart
messages, former code works fine too.
it took me quite while sort out haven't found example or documentation scenario anywhere. helps people facing same problem had, or @ least helping them narrow down other problems.
Comments
Post a Comment