Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: HTTP Post


This question is answered.


Permlink Replies: 2 - Last Post: Feb 26, 2018 11:06 AM Last Post By: Buck Ard
Buck Ard

Posts: 52
Registered: 8/2/09
HTTP Post  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2018 12:04 PM
Hi, I'm trying to connect to a server and get information with http post.

This is the code to send
<REQUEST>
      <LOGIN authenticationkey="myapikey" />
      <QUERY objecttype="TrainStation">
            <FILTER />
      </QUERY>
</REQUEST>


I'm doing it in Delphi like this
procedure TForm1.Button1Click(Sender: TObject);
var
  Data: TStringList;
begin
  Data := TStringList.Create;
  try
    Data.Add(
    '<REQUEST>'+
      '<LOGIN authenticationkey="myapikey" />'+
      '<QUERY objecttype="TrainStation">'+
            '<FILTER />'+
      '</QUERY>'+
    '</REQUEST>');
    Memo1.Lines.Text := IdHTTP1.Post('url', Data);
  finally
    Data.Free;
  end;
 
end;


But the respond I get is an error "Unsupported request content type".
How can I setup this in a corret way?
--
Buck
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: HTTP Post
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2018 9:22 AM   in response to: Buck Ard in response to: Buck Ard
Buck Ard wrote:

But the respond I get is an error "Unsupported request content type".

You are using the overloaded version of TIdHTTP.Post() that is meant
for HTML webform submissions. It sets the request's 'Content-Type'
header to 'application/x-www-form-urlencoded', which is what the server
is complaining about.

How can I setup this in a corret way?

You need to use the TStream overloaded version of TIdHTTP.Post()
instead, and set the TIdHTTP.Request.ContentType property to whatever
the server is expecting you to send ('application/xml', perhaps?):

procedure TForm1.Button1Click(Sender: TObject);
var
  Data: TStringStream;
begin
  Data := TStringStream.Create(
    '<REQUEST>'+
      '<LOGIN authenticationkey="myapikey" />'+
      '<QUERY objecttype="TrainStation">'+
        '<FILTER />'+
      '</QUERY>'+
    '</REQUEST>',
    TEncoding.UTF8);
  try
    IdHTTP1.Request.ContentType := ...;
    IdHTTP1.Request.CharSet := 'utf-8';
    Memo1.Lines.Text := IdHTTP1.Post('url', Data);
  finally
    Data.Free;
  end;
end;


--
Remy Lebeau (TeamB)
Buck Ard

Posts: 52
Registered: 8/2/09
Re: HTTP Post  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2018 11:06 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you, works fine.
--
Buck
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02