Board index » delphi » How to post data with Indy Http client

How to post data with Indy Http client

This is a small exaple of using post to send data to web server.

There is two different ways to do this operation.

Example 1:
<----------------------------------------------------------------->
procedure TForm1.SendPostData;
Const
  CRLF = #13#10;
Var
  aStream: TMemoryStream;
  Params: TMemoryStream;
  S: String;
begin
  aStream := TMemoryStream.create;
  Params := TMemoryStream.Create;

  HTTP.Request.ContentType := 'multipart/form-data;
boundary=-----------------------------7cf87224d2020a';

  try
    S := '-----------------------------7cf87224d2020a' + CRLF +
      'Content-Disposition: form-data; name="sys_return_url"' + CRLF + CRLF
+
    'hello' + CRLF +
    '-----------------------------7cf87224d2020a--';

    Params.Write(S[1], Length(S));

    with HTTP do begin
      try
        HTTP.Post('http://www.mydomain.com/postexampe.cgi', Params,
aStream);
      except
        on E: Exception do
          showmessage('Error encountered during POST: ' + E.Message);
      end;
    end;
  aStream.WriteBuffer(#0' ', 1);
  showmessage(PChar(aStream.Memory));
  except
  end;
end;
<----------------------------------------------------------------->

Example 2:
<----------------------------------------------------------------->
procedure TForm1.SendPostData;
Var
  aStream: TMemoryStream;
  Params: TStrings;
begin
  aStream := TMemoryStream.create;
  Params := TStringlist.create;
  HTTP.Request.ContentType := 'application/x-www-form-urlencoded';

  try
    Params.add('sys_return_url=' + 'helo');
    with HTTP do begin
      try
        HTTP.Post('http://www.mydomain.com/postexampe.cgi', Params,
aStream);
      except
        on E: Exception do
          showmessage('Error encountered during POST: ' + E.Message);
      end;
    end;
  aStream.WriteBuffer(#0' ', 1);
  showmessage(PChar(aStream.Memory));
  except
  end;
end;
<----------------------------------------------------------------->

As you can see there is a difference in the way post stream is constructed
and the ContentType. In the first example ContentType is
"multipart/form-data; boundary=-----------------------------7cf87224d2020a"
and this boundary is used to separate different parameters.

In the second example the ContentType is
"application/x-www-form-urlencoded".
In this case the paremeteras are passed in the form

ParamName=ParamValue

and each line is different parameter.

Where these two formats of post information are used?

The first one is used when you have binary data to post and the second one
is when you are going to post only text fields.

If you have any questions please fill free to send me e-mail or post them on
this newsgroup.

Doychin
doic...@5group.com

 

Re:How to post data with Indy Http client


Hi Doychin,

Do you know anything about that (random) number in the boundary part. Are
there any limits whitin it must be or is it just a 14 digit hex number
000000000000000 to FFFFFFFFFFFFFF or does it have to be in a specific range?

If so, any ideas of how to generate one in delphi ?

/David

Quote
Doychin Bondzhev <doic...@5group.com> wrote in message

news:3a39d1d8_2@dnews...
Quote
> This is a small exaple of using post to send data to web server.

> There is two different ways to do this operation.

> Example 1:
> <----------------------------------------------------------------->
> procedure TForm1.SendPostData;
> Const
>   CRLF = #13#10;
> Var
>   aStream: TMemoryStream;
>   Params: TMemoryStream;
>   S: String;
> begin
>   aStream := TMemoryStream.create;
>   Params := TMemoryStream.Create;

>   HTTP.Request.ContentType := 'multipart/form-data;
> boundary=-----------------------------7cf87224d2020a';

>   try
>     S := '-----------------------------7cf87224d2020a' + CRLF +
>       'Content-Disposition: form-data; name="sys_return_url"' + CRLF +
CRLF
> +
>     'hello' + CRLF +
>     '-----------------------------7cf87224d2020a--';

>     Params.Write(S[1], Length(S));

>     with HTTP do begin
>       try
>         HTTP.Post('http://www.mydomain.com/postexampe.cgi', Params,
> aStream);
>       except
>         on E: Exception do
>           showmessage('Error encountered during POST: ' + E.Message);
>       end;
>     end;
>   aStream.WriteBuffer(#0' ', 1);
>   showmessage(PChar(aStream.Memory));
>   except
>   end;
> end;
> <----------------------------------------------------------------->

> Example 2:
> <----------------------------------------------------------------->
> procedure TForm1.SendPostData;
> Var
>   aStream: TMemoryStream;
>   Params: TStrings;
> begin
>   aStream := TMemoryStream.create;
>   Params := TStringlist.create;
>   HTTP.Request.ContentType := 'application/x-www-form-urlencoded';

>   try
>     Params.add('sys_return_url=' + 'helo');
>     with HTTP do begin
>       try
>         HTTP.Post('http://www.mydomain.com/postexampe.cgi', Params,
> aStream);
>       except
>         on E: Exception do
>           showmessage('Error encountered during POST: ' + E.Message);
>       end;
>     end;
>   aStream.WriteBuffer(#0' ', 1);
>   showmessage(PChar(aStream.Memory));
>   except
>   end;
> end;
> <----------------------------------------------------------------->

> As you can see there is a difference in the way post stream is constructed
> and the ContentType. In the first example ContentType is
> "multipart/form-data;

boundary=-----------------------------7cf87224d2020a"

- Show quoted text -

Quote
> and this boundary is used to separate different parameters.

> In the second example the ContentType is
> "application/x-www-form-urlencoded".
> In this case the paremeteras are passed in the form

> ParamName=ParamValue

> and each line is different parameter.

> Where these two formats of post information are used?

> The first one is used when you have binary data to post and the second one
> is when you are going to post only text fields.

> If you have any questions please fill free to send me e-mail or post them
on
> this newsgroup.

> Doychin
> doic...@5group.com

Re:How to post data with Indy Http client


Sorry but I don't have any idea. Look in the RFC documents for this but in
wich one I don't know.
Please let me know if you find something about that.

Doychin
doic...@5group.com

Quote
"David Bezjak" <davi...@sigma.se> wrote in message

news:91l9e7$2gv4@bornews.inprise.com...
Quote
> Hi Doychin,

> Do you know anything about that (random) number in the boundary part. Are
> there any limits whitin it must be or is it just a 14 digit hex number
> 000000000000000 to FFFFFFFFFFFFFF or does it have to be in a specific
range?

> If so, any ideas of how to generate one in delphi ?

> /David

> Doychin Bondzhev <doic...@5group.com> wrote in message
> news:3a39d1d8_2@dnews...
> > This is a small exaple of using post to send data to web server.

> > There is two different ways to do this operation.

> > Example 1:
> > <----------------------------------------------------------------->
> > procedure TForm1.SendPostData;
> > Const
> >   CRLF = #13#10;
> > Var
> >   aStream: TMemoryStream;
> >   Params: TMemoryStream;
> >   S: String;
> > begin
> >   aStream := TMemoryStream.create;
> >   Params := TMemoryStream.Create;

> >   HTTP.Request.ContentType := 'multipart/form-data;
> > boundary=-----------------------------7cf87224d2020a';

> >   try
> >     S := '-----------------------------7cf87224d2020a' + CRLF +
> >       'Content-Disposition: form-data; name="sys_return_url"' + CRLF +
> CRLF
> > +
> >     'hello' + CRLF +
> >     '-----------------------------7cf87224d2020a--';

> >     Params.Write(S[1], Length(S));

> >     with HTTP do begin
> >       try
> >         HTTP.Post('http://www.mydomain.com/postexampe.cgi', Params,
> > aStream);
> >       except
> >         on E: Exception do
> >           showmessage('Error encountered during POST: ' + E.Message);
> >       end;
> >     end;
> >   aStream.WriteBuffer(#0' ', 1);
> >   showmessage(PChar(aStream.Memory));
> >   except
> >   end;
> > end;
> > <----------------------------------------------------------------->

> > Example 2:
> > <----------------------------------------------------------------->
> > procedure TForm1.SendPostData;
> > Var
> >   aStream: TMemoryStream;
> >   Params: TStrings;
> > begin
> >   aStream := TMemoryStream.create;
> >   Params := TStringlist.create;
> >   HTTP.Request.ContentType := 'application/x-www-form-urlencoded';

> >   try
> >     Params.add('sys_return_url=' + 'helo');
> >     with HTTP do begin
> >       try
> >         HTTP.Post('http://www.mydomain.com/postexampe.cgi', Params,
> > aStream);
> >       except
> >         on E: Exception do
> >           showmessage('Error encountered during POST: ' + E.Message);
> >       end;
> >     end;
> >   aStream.WriteBuffer(#0' ', 1);
> >   showmessage(PChar(aStream.Memory));
> >   except
> >   end;
> > end;
> > <----------------------------------------------------------------->

> > As you can see there is a difference in the way post stream is
constructed
> > and the ContentType. In the first example ContentType is
> > "multipart/form-data;
> boundary=-----------------------------7cf87224d2020a"
> > and this boundary is used to separate different parameters.

> > In the second example the ContentType is
> > "application/x-www-form-urlencoded".
> > In this case the paremeteras are passed in the form

> > ParamName=ParamValue

> > and each line is different parameter.

> > Where these two formats of post information are used?

> > The first one is used when you have binary data to post and the second
one
> > is when you are going to post only text fields.

> > If you have any questions please fill free to send me e-mail or post
them
> on
> > this newsgroup.

> > Doychin
> > doic...@5group.com

Re:How to post data with Indy Http client


Quote
"Doychin Bondzhev" <doic...@5group.com> wrote in message

news:3a3e4c6a_2@dnews...

Quote
> Sorry but I don't have any idea. Look in the RFC documents for this but in
> wich one I don't know.

http://www.rfc-editor.org/rfc/rfc2046.txt

"Boundary delimiters must not appear within the encapsulated material, and
must be no longer than 70 characters, not counting the two leading hyphens."

hth...

Don

Re:How to post data with Indy Http client


Thanks ;-))

Quote
"Don" <dsid...@charter.net> wrote in message news:3a3e518c_1@dnews...

> "Doychin Bondzhev" <doic...@5group.com> wrote in message
> news:3a3e4c6a_2@dnews...
> > Sorry but I don't have any idea. Look in the RFC documents for this but
in
> > wich one I don't know.

> http://www.rfc-editor.org/rfc/rfc2046.txt

> "Boundary delimiters must not appear within the encapsulated material, and
> must be no longer than 70 characters, not counting the two leading
hyphens."

> hth...

> Don

Other Threads