RTC Forums

Subscription => Support => Topic started by: ecilia on January 26, 2015, 11:32:21 AM



Title: javascript JSON <--> TRtcHttpServer
Post by: ecilia on January 26, 2015, 11:32:21 AM
Hi,

I need to create a Delphi XE7 software which receive JSON data from javascript and resend json data in response.

What's the better way to do that please ?

Thanks,


Title: Re: javascript JSON <--> TRtcHttpServer
Post by: D.Tkalcec (RTC) on January 26, 2015, 11:43:46 AM
For receiving requests and sending responses containing any kind of data (the actual format is irrelevant), you would use the TRtcDataRequest component on the Server. For parsing received data into JSON objects and for generating JSON data as a response, you can use any JSON-capable Library, or you can use the TRtcValue class from the rtcInfo unit.

You can find an example using the TRtcRecord class to generate a simple JSON response to a HTTP request in the  "QuickStart/SimpleJSONServer" folder. If you look at that example, you will see that the OnDataReceived checks for "Request.Complete" before doing anything, after which you will get the complete request content body by using the "Read" or "ReadEx" method on the Sender:TRtcConnection object passed to the event. You can parse the request using any JSON-capable library accepting a String (Read) or a Byte Array (ReadEx) and send out a response with the "Write" (String) or "WriteEx" (Byte Array) method.

PS. There are already a number of topics about JSON on this Forum. Just use the Search button above, enter "JSON" and you will find them.

Best Regards,
Danijel Tkalcec


Title: Re: javascript JSON <--> TRtcHttpServer
Post by: ecilia on January 26, 2015, 02:30:20 PM
Thanks for your help !

I have found HelloWorldJSONServer and I'm ok for TRtcRecord to return data.

But I don't understand how to get  values from POST ou GET request with TRtcDataRequest in OnDataReceive.

If I wait for a POST request with parameter MyValue which contain JSON data , how to use TRtcDataRequest to get value ?

Sorry for this newbie question, but I'm late for development  :-[

If I use Read or ReadEx to test body, I get an empty string when I test with http://localhost/service/helloworld?myvalue=12 .



Title: Re: javascript JSON <--> TRtcHttpServer
Post by: D.Tkalcec (RTC) on January 26, 2015, 03:14:38 PM
Using Read or ReadEx method on the Sender object will give you the content body (not the header) currently stored in that connections receiving buffer and clear the buffer immediately. This gives you the flexibility to use the Read or ReadEx method in every event as data arrives and store "chunks" in some specific place (for example, in a file or in the Request.Params object for parsing the form POST data) or ... to wait for the complete request to arrive (Request.Complete), then use the Read or ReadEx method once to get the whole content body in a single variable and use it for parsing that content with some specific library (for example, when receiving direct JSON requests from JavaScript).

Your questions are very basic, which makes me wonder if you even finished the very first few Quick Start lessons. If you haven't, please finish at least these Server-side lessons:
Server Lesson 1: Write your first RTC HTTP Server (http://www.realthinclient.com/sdkarchive/index74fc74fc.html)
Server Lesson 2: Sending dynamically generated content (http://www.realthinclient.com/sdkarchive/index48d948d9.html)
Server Lesson 2b: Using Query parameters (http://www.realthinclient.com/sdkarchive/indexc55cc55c.html)
Server Lesson 3: Sending small files from a folder (http://www.realthinclient.com/sdkarchive/index00b300b3.html)
Server Lesson 4: Sending large files out (http://www.realthinclient.com/sdkarchive/index86dc86dc.html)

When you are finished with the lessons I've listed above, take a look in the "QuickStart" folder at the "BrowserUpload" and the "SimpleJSONServer" examples Projects. One is about writing a Server to host a Web site receiving Form POST data from a Web Browser, the other one is about responding to JSON requests (as said above).

For more Quick Start lessons, check - HERE (https://rtcforum.teppi.net/index.php?topic=12.0).

By the way ... GET requests do not have a content body. Anything you enter into the Address field of any Web Browser will be sent as a GET request to the Server, which is why using the Read or ReadEx method will return an empty result. Request headers (everything inside the HTTP request line and all the header variables sent before the content body) are stored in the TRtcDataServer(Sender).Request object, which you have full access to already from the "OnCheckRequest" event on the TRtcDataReceived component.

Best Regards,
Danijel Tkalcec