Recent

Author Topic: [solved]Http Error 409 when using indy  (Read 3119 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 792
    • Blog personal
[solved]Http Error 409 when using indy
« 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
Code: [Select]
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
Code: [Select]
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
« Last Edit: June 14, 2018, 04:48:05 pm by BlueIcaro »

BlueIcaro

  • Hero Member
  • *****
  • Posts: 792
    • Blog personal
Re: Http Error 409 when using indy
« Reply #1 on: June 12, 2018, 08:38:18 pm »
Reading information about that industrial machine, I found:
Quote
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

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Http Error 409 when using indy
« Reply #2 on: June 13, 2018, 08:21:50 pm »
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:

Code: [Select]
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;
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 792
    • Blog personal
Re: Http Error 409 when using indy
« Reply #3 on: June 14, 2018, 04:47:55 pm »
Thanks Remy, it's works fine!.

/BlueIcaro

 

TinyPortal © 2005-2018