Alex Rivera | Logout

What’s the simplest way to call Http POST url using Delphi?

Asked 2008-11-19T14:17:22.493
37

Inspired by the question What’s the simplest way to call Http GET url using Delphi? I really would like to see a sample of how to use POST. Preferably to receive XML from the call.

Added: What about including an image or other file in the post data?

Edit
Report

1 Answer

19

Here's an example of using Indy to Post a JPEG to a webserver running Gallery

I've got more examples of this sort of stuff (I use them in a screensaver I wrote in Delphi for the Gallery project available here, or more info on the Gallery website here).

The important bit I suppose is that the JPEG gets passed in as a stream.

procedure AddImage(const AlbumID: Integer; const Image: TStream; const ImageFilename, Caption, Description, Summary: String);
var
  Response: String;
  HTTPClient: TidHTTP;
  ImageStream: TIdMultipartFormDataStream;
begin

  HTTPClient := TidHTTP.Create;

  try
    ImageStream := TIdMultiPartFormDataStream.Create;
    try
      ImageStream.AddFormField('g2_form[cmd]', 'add-item');
      ImageStream.AddFormField('g2_form[set_albumId]', Format('%d', [AlbumID]));
      ImageStream.AddFormField('g2_form[caption]', Caption);
      ImageStream.AddFormField('g2_form[force_filename]', ImageFilename);
      ImageStream.AddFormField('g2_form[extrafield.Summary]', Summary);
      ImageStream.AddFormField('g2_form[extrafield.Description]', Description);

      ImageStream.AddObject('g2_userfile', 'image/jpeg', Image, ImageFilename);

      Response := HTTPClient.Post('http://mygallery.com/main.php?g2_controller=remote:GalleryRemote', ImageStream);
    finally
      ImageStream.Free;
    end;
  finally
    HTTPClient.Free;
  end;
end;
answered 2009-06-08T21:04:11.097

Your Answer