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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...