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?

Community
  • 1
  • 1
Tom
  • 1,381
  • 3
  • 15
  • 26

3 Answers3

51

Using Indy. Put your parameters in a StringList (name=value) and simply call Post with the URL and StringList.

function PostExample: string;
var
  lHTTP: TIdHTTP;
  lParamList: TStringList;
begin
  lParamList := TStringList.Create;
  lParamList.Add('id=1');

  lHTTP := TIdHTTP.Create;
  try
    Result := lHTTP.Post('http://blahblahblah...', lParamList);
  finally
    lHTTP.Free;
    lParamList.Free;
  end;
end;
Bruce McGee
  • 15,076
  • 6
  • 55
  • 70
  • TIdHTTP support also https? – Gabriel Jul 01 '14 at 13:36
  • 1
    Are the parameters from `lParamList` same as unparsed params of a request to delphi http server (I' m trying to send a post request to already created delphi http server) – bob_saginowski Oct 02 '14 at 13:15
  • 1
    I want to execute further course of action on the website itself after posting parameters to the website, how it can be done ? IS it possible using above method ? – Ninad Avasare Dec 27 '17 at 18:21
  • @Ninad: Yes, this is possible. Just call Post or Get again before freeing the TIdHTTP object. – Bruce McGee Dec 27 '17 at 20:07
17

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;
Conor Boyd
  • 1,024
  • 7
  • 15
8

Again, Synapse TCP/IP library to the rescue. Use the HTTPSEND routine HTTPPostURL.

function HttpPostURL(const URL, URLData: string; const Data: TStream): Boolean;

Your URL would be the resource to post too, the URLDATA would be the form data, and your XML results would come back as a stream in DATA.

skamradt
  • 15,366
  • 2
  • 36
  • 53