#1 2020-04-01 10:49:01

tomek
Member
Registered: 2017-10-24
Posts: 46

POST request in method-based service

Hi
How to easy process POST request in method-based service, i.e.:
- how to extract parameters from JSON payload?
- request Content type: text/plain or application/json?
- is it possible to use GetInputAsTDocVariant?

Regards, Tomek

Offline

#2 2020-04-01 12:39:21

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: POST request in method-based service

Did you read the documentation about method-based services?

Did you check the corresponding samples?

Offline

#3 2020-04-01 13:28:53

tomek
Member
Registered: 2017-10-24
Posts: 46

Re: POST request in method-based service

Yes, I read:
https://synopse.info/files/html/Synopse … ml#TITL_49
http://blog.synopse.info/post/2010/07/1 … phi-7-2010
and unless I missed something, all of it with corresponding samples are related with GET request and extracting parameters from URI.

Offline

#4 2020-04-01 15:01:03

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: POST request in method-based service

Check the https://synopse.info/files/html/api-1.1 … URICONTEXT documentation.

For the JSON body, you can use either JsonDecode(), or a TDocVariantData.

Offline

#5 2020-04-01 15:26:43

tomek
Member
Registered: 2017-10-24
Posts: 46

Re: POST request in method-based service

procedure TRemoteSrv.Test(Ctxt: TSQLRestServerURIContext);
var
 vBody: RawJSON;
 res: array [0..0] of TValuePUTF8Char;
 sParam: RawUTF8;
begin
  if Ctxt.Method = mPOST then begin
    vBody := Ctxt.Call.InBody;
    JSONDecode(vBody, ['Param1'], @res, true);
    sParam := res[0].Value;
  end;
  Ctxt.Results(['Test: ' + sParam]);
end;

This is what you meant?

Last edited by tomek (2020-04-01 15:27:28)

Offline

#6 2020-04-01 19:52:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: POST request in method-based service

Yes!
smile

And make sure your code is thread-safe, because method-based service execution runs in parallel.

Offline

Board footer

Powered by FluxBB