How to handle TIdHTTPServer with TIdMultiPartFormDataStream

11,046

This has been asked and answered many times before in the Embarcadero and Indy forums. Please search through their archives, as well as other archives, like Google Groups, to find code examples.

In a nutshell, when the TIdHTTPServer.OnCommandGet event is triggered, if the AResponseInfo.ContentType property says multipart/form-data (the version of TIdHTTP.Post() you are using will send application/x-www-form-urlencoded instead), the AResponseInfo.PostStream property will contain the raw MIME data that the client posted. You can use the TIdMessageDecoderMIME class to parse it. However, that class was never intended to be used server-side, so it is not very intuitive to use, but it is possible nontheless.

In Indy 11, I am planning on implementing native multipart/form-data parsing directly into TIdHTTPServer itself, but there is no ETA on that yet as we have not started work on Indy 11 yet.

Share:
11,046
XBasic3000
Author by

XBasic3000

Electronics and Programming is my life. Im a kind of person that when i see things i can see there is something to improve. I have always solutions to the problem.

Updated on July 11, 2022

Comments

  • XBasic3000
    XBasic3000 almost 2 years

    Hi i need help on how to retrieved the parameters and data using IdHttpServer from indy.

    many of my application uses TIdMultiPartFormDataStream to send data over the php. I would like to use the TIdHTTPServer to verify parameters for some reason and forward the request to its destination.

    i created a short example for you to see.

    uses
      IdContext, IdMultipartFormData;
    
    // Server Side------------------------------------------------
    
    IdHTTPServer1.Defaultport := 88;
    IdHTTPServer1.active := True;
    
    procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
    begin
      // the request will be pass through its destination by POST/GET
      // and send the result back to the client apps.
      AResponseInfo.ContentText := ARequestInfo.Params.Text;
    end;
    
    // Client Side------------------------------------------------
    // This will work using the standard Post or Get
    procedure TForm1.btnPost1Click(Sender: TObject);
    var
      sl: TStringList;
      res: String;
    begin
      sl := TStringList.Create;
      try
        sl.Add('Param1=Data1');
        sl.Add('Param2=Data1');
        sl.Add('Param3=Data2');
        sl.Add('Param4=Data3');
        res := IdHTTP1.Post('http://localhost:88/some.php', sl);
        ShowMessage(res);
      finally
        sl.Free;
      end;
    end;
    
    //how can i get the parameters and value for this code in my IdHttpServer
    procedure TForm1.btnPost2Click(Sender: TObject);
    var
      mfd: TIdMultiPartFormDataStream;
      res: String;
    begin
      mfd := TIdMultiPartFormDataStream.Create;
      try
        mfd.AddFormField('Param1', 'Data1');
        mfd.AddFormField('Param2', 'Data1');
        mfd.AddFormField('Param3', 'Data2');
        mfd.AddFormField('Param4', 'Data3');
        res := IdHTTP1.Post('http://localhost:88/some.php', mfd);
        ShowMessage(res);
      finally
        mfd.Free;
      end;
    end;
    

    and how would i know if the Client apps pass a TIdMultiPartFormDataStream type of parameter?