Post method and indy
lazarus / 38-Networking and Web Programming
----------------------------------------------------------------------
BlueIcaro · 2016-11-10 16:55
Hi, I connected my lazarus aplication to a webserver (it's a websever
embeded into a industrial machine). I send request using get method,
and I haven't any problem.
But I can send data using POST method. The server send me the
following form:
controller
The information of the server, says that using Post method I can send
some option from de form above.
The documention sever gives me this example, about using the form
above:
Quotecurl --digest -u "Default User":robotics -d "restart-mode=xstart"
-X POST "http://localhost/ctrl"
I wrote in my lazarus aplication this:
Code: [Select]procedure TForm1.Button1Click(Sender: TObject);
var
t: string;
Fichero: TStringList;
r: TIdHTTPResponse;
begin
Fichero := TStringList.Create;
try
try
Fichero.Text := IdHTTP1.Post('http://localhost/ctrl', 'restart-
mode=restart');
r := IdHTTP1.Response;
Memo1.Lines.Text := Fichero.Text;
Memo2.Lines.Add(R.ResponseText);
except
On E: Exception do
ShowMessage(E.message);
end;
finally
FreeAndNil(Fichero);
end;
end;
But I think I got a Exception and the Shows this message: 'Unable to
open file "restart-mode=restart"'
I think I'm sending wrong data, but I don't know why.
Any idea?
thanks in advance
/BlueIcaro
P.D: I'm using Lazarus 1.6 (last oficial release) and W10. Indy
version: '10.6.2.0'
----------------------------------------------------------------------
Remy Lebeau · 2016-11-10 19:08
Quote from: BlueIcaro on November 10, 2016, 05:55:56 pmBut I think I
got a Exception and the Shows this message: 'Unable to open file
"restart-mode=restart"'
That is because you are using the wrong TIdHTTP.Post() overload. You
are using the overload that takes a filename as input:
Code: [Select]function Post(AURL: string; const ASourceFile: String
{$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding =
nil{$ENDIF}
): string; overload;
When posting an HTML webform, you need to use the overload that takes
either a TStrings or TIdMultiPartFormDataStream as input, depending on
the "enctype" attribute of the webform (TStrings for
"application/x-www-form-urlencoded", TIdMultiPartFormDataStream for
"multipart/form-data"):
Code: [Select]function Post(AURL: string; ASource: TStrings;
AByteEncoding: IIdTextEncoding = nil
{$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil;
ADestEncoding: IIdTextEncoding = nil{$ENDIF}): string; overload;
Code: [Select]function Post(AURL: string; ASource:
TIdMultiPartFormDataStream
{$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding =
nil{$ENDIF}
): string; overload;
In your case, the webform does not specify an "enctype", so
"application/x-www-form-urlencoded" is the default, eg:
Code: [Select]procedure TForm1.Button1Click(Sender: TObject);
var
t: string;
Params: TStringList;
begin
try
Params := TStringList.Create;
try
Params.Add('restart-mode=restart');
t := IdHTTP1.Post('http://localhost/ctrl', Params);
finally
Params.Free;
end;
Memo1.Lines.Text := t;
Memo2.Lines.Add(IdHTTP1.ResponseText);
except
on E: Exception do
ShowMessage(E.message);
end;
end;
----------------------------------------------------------------------
BlueIcaro · 2016-11-11 11:17
Hi, thanks Remy Lebeau, it's work find.
I didn't see that I was using a wrong overload method
/BlueIcaro