RTC Forums
May 09, 2024, 09:28:46 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: dummie question: How to handle POST at the server side  (Read 6545 times)
WilliamY
RTC License++
*****
Posts: 23


« on: September 02, 2011, 02:14:16 AM »

HI, Danijel

Sorry for the question since I'm really new to web stuff.   If a browser send a POST with XML data to the server, how does the server get the XML data, using TrcDataProvider,  Do you have any demos about it?

Thanks,

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


« Reply #1 on: September 02, 2011, 02:40:41 AM »

There is more than one way to send data from a Browser to a Server.

A) If you have used JavaScript to send the request with XML content, the Server will get that content in "raw" format and you can use the "Read" method in the "OnDataReceived" event (RtcDataProvider component) on the Server to get it. Here is an example "OnDataReceived" event implementation:

var
  Srv:TRtcDataServer absolute Sender;
  MyXML:AnsiString;
begin
  if Srv.Request.Complete then // wait until the complete request is here
    begin
    MyXML := Srv.Read;
    // Now you have the XML in the "MyXML" variable
    end;
end;

B) If you have used a HTML FORM with an UPLOAD field and a SUBMIT button, take a look at the "QuickStart\BrowserUpload\BrowserUpload.dpr" Project. It shows how to process data received as FORM POST from a Web Browser.

Best Regards,
Danijel Tkalcec
Logged
WilliamY
RTC License++
*****
Posts: 23


« Reply #2 on: September 02, 2011, 05:03:34 AM »

Hi, Danijel

Do I need to check if the method = 'POST', before calling 'Read' method? :

var
  Srv:TRtcDataServer absolute Sender;
  MyXML:AnsiString;
begin
  if Srv.Request.Complete then // wait until the complete request is here
    if Srv.Request.Method = 'POST' then
    begin
    MyXML := Srv.Read;
    // Now you have the XML in the "MyXML" variable
    end;

Regards,

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


« Reply #3 on: September 02, 2011, 12:51:23 PM »

Yes, it would be wise to check the Request Method and any other Request parameters you are expecting from the Client/Browser, especially if you are using the same TRtcDataProvider to process more than one request type. Naturally, you will also need to implement the "OnCheckReqest" event to accept the request. The code I've posted only shows the minimum you have to do in the "OnDataReceived" event to get the content body in one piece.

Best Regards,
Danijel Tkalcec
Logged
WilliamY
RTC License++
*****
Posts: 23


« Reply #4 on: September 02, 2011, 04:38:02 PM »

Hi, Danijel

in the 'OnCheckRequest',  Should I check the FileName or FilePath for POST method?   

Regards,

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


« Reply #5 on: September 02, 2011, 04:48:06 PM »

The "Request.FileName" property gives you the complete URI in a single Ansi String, while FilePath separates the URI in individual elements. Both properties contain the same data but in different formats, so it is entirely up to you what you want to use. Technically speaking, there is no difference for the RTC SDK between a POST, GET or any other Request Method. All the properties you have access to with a GET method will also be there for POST or any other request method.

PS. If you haven't done so already, I strongly suggest going through all the Quick Start lessons in the "Quick Start" forum section. They contain a lot of general information about the RTC SDK. Another good source of information is the FAQ section. Quick Start and FAQ are there to get you familiar with the RTC SDK and should answer almost all "beginner" questions. There is also a lot of detailed information about properties, methods and events in source code comments. If you are unsure what a certain property does, Ctrl-Click on it in the Delphi IDE and you should get to the interface section where the property/event/method is declared and explained. Another way to get more details on each property/method/event is to check the HELP file, which is generated from source code comments.

Best Regards,
Danijel Tkalcec
Logged
WilliamY
RTC License++
*****
Posts: 23


« Reply #6 on: September 02, 2011, 05:16:54 PM »

Thanks much!

William
Logged
WilliamY
RTC License++
*****
Posts: 23


« Reply #7 on: September 03, 2011, 03:43:04 AM »

HI, Danijel

How would I detect that the POST is send from Javascript or FORM POST, so I can handle them properly in the OnDataReceived event,  I took a look at the BrowserUpload demo and your example A, the implementation is totally different, one need to call Params.AddText(...), one is not.  sorry for the question since I'm not a HTTP expert.

Regards,

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


« Reply #8 on: September 03, 2011, 09:58:00 AM »

Since you are writing the Server, you should actually know what you expect from the Client. To receive a FORM POST request, you need to send the Client a HTML file which would return a FORM POST request after a SUBMIT, like the BrowserUpload example does. If you are NOT sending the Client such a HTML file, then your shouldn't get a FORM POST request. But if you want to be sure that the content received is what you expect, look at Request headers (Request[...] property) and/or analyze the content body received (what you get when using the "Read" method).

The "Content-Type" HTTP header ("Request.ContentType" or "Request['Content-Type']" property) normally includes information about the type of the content you have received (FORM POST requests sent by the Browser usually have a Content-Type starting with 'multipart/form-data'), but you can also look at the content body (XML content usually starts with '<?xml').

I think the real question is what you WANT to do. Do you want to write a Web Application where users (people) will get to see a web page (HTML like in the BrowserUpload example) and use that page to UPLOAD files to your Server, or do you want to providing an interface through which 3rd-party Client Applications (or your own RTC Clients) will be sending you files as "raw" content (XML, XML-RPC, SOAP, JSON)?

If you want users (people) to upload files to your Server by using a Web Browser, the easiest way to do this is by sending out a HTML file with a FORM and an UPLOAD field, like shown in the BrowserUpload demo. This way, your Browser-side implementation vers simple. It is all inside the HTML (no JavaScript needed) and you can use the Params property to access all submitted fields and files.

When you receive the content as a FORM POST request (Multipart/Form-Data), there will be a delimiter specified in the Content-Type, and the Content body will contain formatting specific to the "multipart/form-data" format. This format allows the Web Browser to send multiple fields or files in a single request. But it also requires parsing the content body (this is what the "Params" property can do for you in the RTC SDK), which makes this format less suitable for communication between two Applications and should ONLY be used when you want users (a person) to use a simple Web Browser interface (defined in your HTML file) to send files to your Server.

On the other hand, if you want to provide an interface through which 3rd-party Client Applications or your own RTC Clients (not users from a simple HTML-based Web Browser interface) will be able to send files to your Server, you should use a format which is more "down to the metal", allowing Clients to send you raw file content in the content body, like it is the case in XML-RPC, JSON, SOAP and most other formats meant to be used for communication between machines. In that case, you will usually specify a different URL for each content type you want to receive (use a separate TRtcDataProvider component for each format you want to poces, accepting different "Request.FileName" or "Request.FilePath" inside the "OnCheckRequest" event) and will get "raw files" in the content body without Browser-specific "envelopes" or formatting, as in the "QuickStart\ClientUpload" example Project included in the RTC SDK.

Or ... you will use RTC remote functions, which also support the XML-RPC format for communicating with 3rd-party Clients and Servers.

PS. If you need more help with web standards (like HTTP, HTML or XML), I strongly suggest searching the web (Google is your friend), where you will find a lot of freely available and very extensive information. There is absolutely nothing RTC-specific about these standards and if you are serious about developing Web Applications, you will need to learn them sooner or later, regardless of the language and/or components you use for development.

Best Regards,
Danijel Tkalcec
Logged
WilliamY
RTC License++
*****
Posts: 23


« Reply #9 on: September 03, 2011, 03:01:06 PM »

HI, Danijel

What I'm doing now is trying to develop a Web interface (API) for one of our product so that any 3rd parties can call to build their own web applications, what APIs return to them are always in XML, e.g. if I define a file name called 'Registration', I expect 3rd parties can POST the data in XML include all the registration information, so I can parse the XML at server end and save into the database.

Our produce is a widely-used desktop application, its a 3-tier systems, and now we are trying moving some of the functions to Web based, The Web APIs are built in the App Server, so that any of our customers can call and build their own web applications/web sites.

Regards,

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


« Reply #10 on: September 03, 2011, 03:13:30 PM »

In that case, the simple example I've posted in my 1st reply, together with the appropriate "OnCheckRequest" implementation (depending on which requests you want to process) is all you really need. The XML content posted by a 3rd-party Client will be in the "MyXML" variable which you can parse and process as needed. You can also take a look at Client and Server projects from the "QuickStart\ClientUpload" folder, which show you how to send a file from a RTC Client and receive it on a RTC Server for an example on sending and receiving files in a "raw" format.

Best Regards,
Danijel Tkalcec
Logged
WilliamY
RTC License++
*****
Posts: 23


« Reply #11 on: September 04, 2011, 04:50:44 AM »

Thanks. much appreciated!

William
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.027 seconds with 18 queries.