Jump to content
Ricardo

JSON Post

Recommended Posts

Hi All. This is my first post.

I need help to start my first test project.

I want to send and receive json objects to a server socket on port 8082.

The json structure to send is like this: {"Command":"Open","Table":"Client","Code":"XXXX"}

The json structure to reveive is like this: {"Code":"XXXX", "Name":"Name of client",.....}

My version of SMS is 3.0.0.201

Thanks!!

 

 

Share this post


Link to post
Share on other sites

Something like this:

uses
... SmartCL.Net.Http, System.Codec.Url, System.JSON;

type
...

  TMyJsonCommand = record
    Command: String;
    Table: String;
    Code: String;
  end;

...
procedure SendAndReceiveExample;
var JsonToSend: TMyJsonCommand;
begin
  var httpcli := TW3HttpRequest.Create;
  httpcli.OnDataReady := procedure(Sender: TW3HttpRequest)
    var JsonData: Variant;
    begin
      JsonData := TJson.parse(Sender.ResponseText);
      WriteLn('Code received: '+JsonData.Code);
      WriteLn('Name received: '+JsonData.Name);
    end;
    httpcli.Open('POST','https://example.com:8082/wherever.it.is');
    httpcli.RequestHeaders['Content-type']:='application/x-www-form-urlencoded';

    JsonToSend.Command := 'Open';
    JsonToSend.Table := 'Client';
    JsonToSend.Code := 'XXXX';
    httpcli.Send('json='+TURLCodec.Encode(TJson.stringify(JsonToSend)));
end;

 

And if you get this to work locally but not on mobile devices, you solve it on your server side by adding the necessary CORS headers.

Share this post


Link to post
Share on other sites

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

×