RTC Forums
May 09, 2024, 12:40:03 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: javascript JSON <--> TRtcHttpServer  (Read 3913 times)
ecilia
Newbie
*
Posts: 2


« 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,
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 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
Logged
ecilia
Newbie
*
Posts: 2


« Reply #2 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  Embarrassed

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

Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 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
Server Lesson 2: Sending dynamically generated content
Server Lesson 2b: Using Query parameters
Server Lesson 3: Sending small files from a folder
Server Lesson 4: Sending large files out

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.

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
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 17 queries.