Link to home
Start Free TrialLog in
Avatar of SteveFarndon2000
SteveFarndon2000Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Using POST with Indy 10 TIdHTTP

Hello Experts,

This will be simple for the one who's done this before. It's driving me nuts trying to spot the right method/property to make this work. The Indy 10 documentation is ...er...weak on examples.

All I want to do is to have a TIdHTTP object (Indy 10 that came with Delphi 2006) do a POST request. Please see below the code I have tried so far. I just get "HTTP /1.1 500 Internal server error" when it tries to execute h.post.

I'm losing too much time hence the high points tariff.

Please help!
function  TTAService.SendSMS(const Destination: WideString; const Body: WideString): Integer;
var
   h : TIdHTTP;
  URL: string;

  Params: TStringStream;
  res: TStringStream;

begin

   try
      h := TIdHTTP.create()
      Params := TStringStream.Create('');
      Params.WriteString(HTTPEncode('Billing_Ref=KUDEX'+'&'));
      Params.WriteString(HTTPEncode('DestinationEx='+Destination+'&'));
      Params.WriteString(HTTPEncode('Body='+Body+'));

      URL := 'http://ws.textanywhere.net/HTTPRX/SendSMSEx.aspx';
      h.Request.Method :=  'POST';
      h.Request.Source := Params;
      h.Request.ContentType := 'application/x-www-form-urlencoded';
      res := TStringStream.create('');
      h.ConnectTimeout := 20000;
      h.post(URL,params,res);

  except
    on E: Exception do begin
      ShowMessage('Error: ' + E.Message);
    end;
  end;

  Params.Free;
  h.free()
end;

Open in new window

Avatar of Mahdi78
Mahdi78
Flag of Algeria image

This is better and worked with me fine to post data to php script, you need TIdhttp, TMemo and TButton,
you will use memo to receive response of your request

 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    IdHTTP1: TIdHTTP;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses IdMultipartFormData;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var data: TIdMultiPartFormDataStream;
begin
data := TIdMultiPartFormDataStream.Create;
 try
  data.AddFormField('user_name', 'value1');
  data.AddFormField('user_password', 'value2');
  Memo1.Text :=IdHTTP1.Post('Link op php or asp', data);
 finally
 data.Free;
 end;

end;

end.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SteveFarndon2000

ASKER

Thanks for the solution. You get the points because you implicitly identified that the 'HTTPEncode' function was the problem although it would have been more convincing if you had said this out loud. Some more explanation about how the TIdHTTP object works in relation to posting would have been even better (to show understanding).
Thanks again for your help.
@SteveFarndon2000
My method failed?,
if case let me know error plz
Mahdi78,

Both solutions worked but neither one of you explained what I was doing wrong or why your solutions would fix the problem. I had to differentiate between the pair of you and Eddie Shipman did (implicitly) highlight the URL encoding issue. I think his solution shows a bit more understanding, and remember that your solution is dependent on the .AddFormField method being bug-free (have you fully tested it?) which is something that Indy components are not reknown for.
Thank you for you reply
To understand my method you will create post.php and put in localhost root, so your post URL will be "http://localhost/post.php"
copy the following script in post.php file

<?
  echo '<b>Your username:</b>'.$_POST['user_name']
.' <br><b>Your password:</b> '. $_POST['password'];
?>

and use Delphi code

uses IdMultipartFormData;

procedure TForm1.Button1Click(Sender: TObject);
var data: TIdMultiPartFormDataStream;
begin
data := TIdMultiPartFormDataStream.Create;
 try
  data.AddFormField('user_name', 'value1');
  data.AddFormField('password', 'value2');
  Memo1.Text :=IdHTTP1.Post('http://localhost/post.php', data);
 finally
 data.Free;
 end;

end;
post method will reply with the following html code

<b>Your username:</b>value1 <br><b>Your password:</b> value2
Avatar of Marie Ganoga
Marie Ganoga

My children were requiring Uia 1025 form last year and were informed about a business that hosts 6,000,000 forms . If people have been needing Uia 1025 form too , here's <code>http://goo.gl/mj9f3C</code>