Lazarus
Programming => Networking and Web Programming => Topic started by: BlueIcaro on June 12, 2018, 06:27:26 pm
-
Hello, I have a industrial machine with a webserver. I used idhttp indy component to "talk" with this machine.
If I use Get method
idhttp1.get ('http://192.168.1.250'); //Machine givesme informations
it's woks fine.
But If i use post method I got: HTTP/1.0 409 Conflict
this is my code
var
t: string;
Params: TStringList;
begin
try
Params := TStringList.Create;
try
Params.Add('restart-mode=restart');
t := IdHTTP1.Post('http://192.168.1.250/' + '/ctrl', Params);
finally
Params.Free;
end;
Memo1.Lines.Text := t;
Memo2.Lines.Add(IdHTTP1.ResponseText);
except
on E: Exception do
begin
ShowMessage(E.message);
memo2.Lines.Text:=E.Message;
end;
end;
But if I use Curl line command it's works fine
curl --digest -u "Default User":robotics -d "restart-mode=restart" -X POST "http://192.168.1.250/ctrl"
I think is some thing on idhttp component that I did wrong.
Any idea?
I use Lazarus 1.8.4 in w10/64 and Indy 10.6.2.0
B.R.
/BlueIcaro
-
Reading information about that industrial machine, I found:
Client applications can update a resource by sending encoded data to the server in the URL, also known as "Form data". The content-type shall be of type application/x-www-form-urlencoded . RFC 3986 defines how reserved character shall be encoded. Most libraries have built in support for character encoding. cUrl uses the content-type www-form-urlencoded as default for POST and PUT. An example of a cUrl call to set (POST) an IO-signal may look like:
curl –digest -u "Default User":robotics -d "lvalue=1" -X POST "http://locahost/rw/iosystem/signals/Virtual/DUNIT/sig1?action=set"
The Form data is comprised of name/value pairs where the name is separated from the value by a '=' character and each name/value pair is separated by an '&' character.
That's way curl command line works and my application with idhttp does not work.
Because I have to use www-form-urlencoded.
How can I do it with idhttp component?
/BlueIcaro
-
You are using the overloaded version of TIdHTTP.Post() that sends webform data in "application/x-www-form-urlencoded" format, so that is not the problem.
Your Indy code is posting to 'http://192.168.1.250//ctrl' (note the double '/' before 'ctrl'), whereas you are having Curl post to 'http://192.168.1.250/ctrl' instead. That extra '/' makes a difference. Make sure you are passing a correct URL to TIdHTTP.
Also, you are having Curl use Digest authentication, but you are not using any authentication in TIdHTTP. Make sure you include the 'IdAuthenticationDigest' unit in your 'uses' clause so Indy enables Digest authentication in TIdHTTP, and then supply values for the TIdHTTP.Request.Username and TIdHTTP.Request.Password properties.
Try this:
var
t: string;
Params: TStringList;
begin
try
IdHTTP1.Request.Username := 'Default User';
IdHTTP1.Request.Password := 'robotics';
IdHTTP1.Request.BasicAuthentication := False;
Params := TStringList.Create;
try
Params.Add('restart-mode=restart');
t := IdHTTP1.Post('http://192.168.1.250/ctrl', Params);
finally
Params.Free;
end;
Memo1.Lines.Text := t;
Memo2.Lines.Add(IdHTTP1.ResponseText);
except
on E: Exception do
begin
ShowMessage(E.message);
Memo2.Lines.Text := E.Message;
end;
end;
end;
-
Thanks Remy, it's works fine!.
/BlueIcaro