Icon Frequently Asked Questions

How do I simulate a form POST operation using the TServerRequest component ?

Let's suppose that we want to submit a user name and password to a PHP application running on the web server, and make it seem to the PHP application that the data can from a normal form submittal. To do so, we would use the following code:

with MyServerRequest do
   begin
   URL := 'https://www.mydomain.com/login.php';
   Method := rmPost;
   RequestHeaders.Values['Content-Type'] := 'multipart/form-data; boundary=AaB03x';
   with RequestContent do
      begin
      Add('--AaB03x');
      Add('Content-Disposition: form-data; name="user"');
      Add('');
      Add(MyUsername);
      Add('--AaB03x');
      Add('Content-Disposition: form-data; name="password"');
      Add('');
      Add(MyPassword);
      Add('--AaB03x');
      end;
   Execute;
   end;

In the above example, MyUserName and MyPassword represent String variables that contain the user name and password, respectively.

Warning Be very careful about sending login information, or any other type of security information, over an un-encrypted connection (http:// vs. https://).

In general, it is much easier to use the HTML Forms functionality to deal with submitting form information in an HTTP POST request.
Image