Jump to content
Die Holländer

Switch MorMot webserver from Get to Post

Recommended Posts

I took over a Delphi Mormot webserver with source and both Get (root) and Post (rootp) are implemented.

 

GET implementation

FModel := TSQLModel.Create([], StringToUTF8(fRoot));
FServer := TSQLRestServerFullMemory.Create(FModel, 'test.json', False, False {true});
FServer.ServicesRouting := TSQLRestRoutingREST2;  

 

POST implementation

FModel_Post := TSQLModel.Create([], StringToUTF8(fRootPost));
FServer_Post := TSQLRestServerFullMemory.Create(FModel_Post, 'test.json', False, False {true});
FServer_Post.ServicesRouting := TSQLRestRoutingJSON_RPC2; //I can change this (to Rest?) if needed because the POST method is not used yet..

 

There is a test function Add and is called by the client as:

localhost:8181/root/<ApplicationName>/Add?n1=1&n2=5 (with http://)

 

Now I want to use the POST method of the server and test it with a small HTTP client program to call the add function.

I have tried a lot of ways but no success.. (Bad Request)

For example:

 

PostData := TStringList.Create;
try
  PostData.Add('{"n1":1,"n2":5}');
  Param:='http://localhost:8181/rootp/<ApplicationName>.Add';

  IdHTTP1.HTTPOptions := IdHTTP1.HTTPOptions + [hoNoProtocolErrorException, hoWantProtocolErrorContent];
  IdHTTP1.Request.ContentType := 'application/json';
  IdHTTP1.Request.Accept := 'application/json';
  IdHTTP1.Request.CacheControl := 'no-cache';

  ReceiveData:=IdHTTP1.POST(Param,PostData);
Finally
  PostData.Free;
End; 

 

What is the proper way to use the POST request ? Thanks.

 

 

Share this post


Link to post
9 hours ago, Die Holländer said:

Param:='http://localhost:8181/rootp/<ApplicationName>.Add';

Should that not be:

Param:='http://localhost:8181/rootp/<ApplicationName>/Add';

Share this post


Link to post

I've tried that but what I understand is that declaration with the slash is when you create

the server with the TSQLRestRoutingREST2 servicerouting instead of TSQLRestRoutingJSON_RPC2.

 

When trying to use parameters the server responds with:

{
"errorCode":406,
"errorText":"sicShared execution failed (probably due to bad input parameters: e.g. did you initialize your input record(s)?) for ScenarioAnalyseTester.Add"
}

 

The POST method is working when the server is started with TSQLRestRoutingREST2  and the whole URL string is like the Get method:

localhost:8181/root/<ApplicationName>/Add?n1=1&n2=5 and leave the parameters empty but

I guess there must be a way to get the result by using the "rootp" server with TSQLRestRoutingJSON_RPC2 and the parameters field of the POST method..

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×