java - Sending a JSON encoded object using Indy and Delphi -


this question has answer here:

i've been working on problem far many hours without making headway. have old solution works, trying port indy make code bit more reliable , easier maintain.

we have servlet handles requests sent using http post messages. messages have single parameter, "command", value of determines servlet should do.

currently have this:

procedure tindyloginserver.sendrequest(command, json: string; request: trequest); var   params: tidstrings;   serverresponse: string; begin   // build parameters   params := tstringlist.create();   params.add('command=' + command);    try     // content type should 'application/json' parameters stop working     findyhttp.request.contenttype := 'application/x-www-form-urlencoded';      serverresponse := findyhttp.post(furl, params);     request.onrequestfinished(serverresponse, '', '');   except     on e: eidhttpprotocolexception     begin       request.onrequestfinished('', e.message, '');     end;     on e: eidsocketerror     begin       if e.lasterror = id_wsaetimedout               request.onrequesttimedout();     end;     on e: eidexception     begin       request.onrequestfinished('', e.message, '');     end;   end; end; 

this sort of works, command gets servlet , starts working expected. problem is, in addition parameters need able send json encoded object along post request. java servlet receives json encoded object using following line of code

final bufferedreader r = req.getreader(); 

the "req" incoming post request , buffered reader later used decode object. can't life of me figure out how attach json string tidhttp instance in way servlet can read data, however.

does have suggestions or examples can at? have found how send file. maybe looking for?

and how can set content type of request 'application/json' without breaking parameter list? if change it, post request still reaches server "command" parameter no longer found.

mostly hint: when use "application/x-www-form-urlencoded" server side expects see key/value pairs like:

key1=value1&key2=value2 etc. 

in case can send json text value of key, example:

{...} params.add('command=' + command); params.add('jsontext=' + json); findyhttp.request.contenttype := 'application/x-www-form-urlencoded';  serverresponse := findyhttp.post(furl, params); {...} 

using "application/json" http server: whole body of request (the part after headers) contain json text.

in php have access "body" of request using "input stream":

$jsontext = file_get_contents("php://input") $jsonobject = json_decode($jsontext ) 

try finding similar in java?

update:

it seems tidstrings prepares request 'application/x-www-form-urlencoded'. remy lebeau suggests here save json string tstream descendant (tmemorystream example) , transfer (i.e. raw). tidhttp.post has override accepts tstream. in case try tidhttp.request.contenttype = 'application/json'. should work way?


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -