RTC Forums
April 20, 2024, 12:20:50 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Sending JSON from a client using POST  (Read 5415 times)
JeffW
RTC License+
****
Posts: 7


« on: March 16, 2018, 11:24:05 AM »

I am having difficulty using POST to send JSON data to a server we have built (using RTC). Essentially, a conversation of JSON is set up. The initial request is handled in the background and the client makes subsequent calls to get a status on the Request. When status is complete, the conversation ends.
I am attempting to use a test client using HTTPClient. In the real world the server is working with a web app based on Apache. I used a code snippet for this forum to test reading JSON (from web sites) and that works as intended:
 var
    Res:TRtcValue;
  begin
  RtcHttpClient1.ServerAddr:='ip.jsontest.com';
  RtcHttpClient1.ServerPort:='80';
  RtcHttpClient1.AutoConnect:=True;

  RtcClientModule1.Client:=RtcHttpClient1;
  RtcClientModule1.DataReqMode:=req_queryNameText;
  RtcClientModule1.DataFormat:=fmt_JSON;

  RtcClientModule1.Prepare('');
  Res:=RtcClientModule1.Execute(False);
  try
    if Res.isType=rtc_Record then
      Label1.Caption:='IP = '+Res.asRecord.asText['ip'];
  finally
    Res.Free;
    end;
  end;

But I have looked through this forum and example apps and it is not immediately clear how I POST a JSON message to the server. I'm sure the answer is simple, so I just need a pointer in the right direction. Code I am attempting to use is below. A write produces an error 105:

procedure TForm2.cxButton3Click(Sender: TObject);
var
  t,s: TRtcRecord;
  Res:TRtcValue;
begin
  t := TRtcRecord.Create;
  t.asText['RequestType'] := 'CommsCheck';
  t.asText['Source'] := 'SourceName';

  MemoRequest.Text := PrettyPrint(t.ToJSON);

  RtcClientModule1.Request.Method := 'POST';

   RTCClientModule1.Request.uri := 'vIQRequest';
   //RtcClientModule1.Prepare('');
   write(t.toJSON);
  Res:=RtcClientModule1.Execute(False);
  try
    if Res.isType=rtc_Record then
    begin
     MemoResponse.Text:=PrettyPrint(Res.asRecord.toJSON);
    end;

  finally
    Res.Free;
  end;
end;

PrettyPrint is a function which reformats the JSON text to a more readable form as I am recording the Requests and responses as they are done up till the JSON 'conversation ends.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: March 16, 2018, 11:33:24 AM »

What is the exact format your Server expects? Is it "JSON-RPC 1.0", or "JSON-RPC 2.0" or some other custom format using "JSON" to encode the content body?
Logged
JeffW
RTC License+
****
Posts: 7


« Reply #2 on: March 16, 2018, 12:01:42 PM »

server is actually based on your server example. It uses HTTPServer and RTCDataProvider. Neither of those appear to have a format in their properties. Data is sent in reply using <TRtcServer>.Write(RtcRecord.toJSON). Since this was designed for a specific purpose, and the client app (written in  Angular JS) we haven't needed to explore the format. We are using SDK V8.04. It doesn't look like we are manually setting either 1.0 or 2.0.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: March 16, 2018, 12:19:28 PM »

Since you are using the TRtcDataProvider component on the Server, which gives you direct access to all the Request headers and the content body unmodified (exactly as received from the Client), your implementation of events on the TRtcDataProvider component defines the actual "format" to be used. In other words, there are no RTC-specific "JSON parameters" involved, since you are the one writing the code to handle the request content and prepare the response.

You can use a TRtcDataRequest component (connected to a TRtcHttpClient component) on the Client to POST a request to the Server and process the response received, as shown in these topics:
POST JSON parameter
How to POST a request?

Also, check Projects in the "Examples/ClientUpload" folder (included in the RTC SDK) for general examples on posting requests with a content body to the Server.
Logged
JeffW
RTC License+
****
Posts: 7


« Reply #4 on: March 16, 2018, 09:03:24 PM »

Thanks.

Newbie question, but now I see how it goes together. My test client now works so I can now simulate flow from the server for the Web-based app that will actually use the server, not to mention other projects.

We got the SDK for a particular project and it seems to be very stable and quick. And once you become aware of the base principles, easy to use.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.024 seconds with 16 queries.