* * *

Author Topic: [SOLVED] LNET HTTPClient - how to POST XML  (Read 3359 times)

Dibo

  • Hero Member
  • *****
  • Posts: 565
[SOLVED] LNET HTTPClient - how to POST XML
« on: July 28, 2010, 10:43:37 pm »
Hi,

I can't find any example how to POST some data. In demos is only simple example how to POST using params (http//www.somesite.com/php?param1=value1&param2=value2...). But, how can I post this XML like in synapse:

Code: (pascal) [Select]
procedure TForm1.buttAddDocumentClick(Sender: TObject);
var
  aHTTP: THTTPSend;
begin
  aHTTP := THTTPSend.Create;
  try
    WriteStrToStream(aHTTP.Document, '<document><title>Test document</title><body>Some text</body></document>');
    aHTTP.MimeType := 'application/xml';

    if aHTTP.HTTPMethod('POST', 'http://localhost:3000/documents.xml') then
    begin
      aHTTP.Document.Position := 0;
      Memo1.Lines.LoadFromStream(aHTTP.Document);
    end else ShowMessage('Can not POST');
  finally
    aHTTP.Free;
  end;
end;  

Regards
« Last Edit: October 23, 2010, 08:46:25 pm by Dibo »

Dibo

  • Hero Member
  • *****
  • Posts: 565
Re: LNET HTTPClient - how to POST XML
« Reply #1 on: October 23, 2010, 08:45:23 pm »
Ok, I very much needed this solution, so I analyze procedure TLHTTPClientSocket.SendRequest. By default, http client does not send absolutely no header (only host), so I sniff what sends synapse and do this same by AddExtraHeader procedure. XML is just new text block in document so I add this by AddExtraHeader too. This is solution code which work, maybe someone need this too:

Code: (pascal) [Select]
  DecomposeURL('h t tp://some.host.com/login.xml', sHost, sUrl, sPort);
  httpclient.Method := hmPost;
  httpclient.Port := sPort;
  httpclient.Host := sHost;
  httpclient.URI := sUrl;
  httpclient.AddExtraHeader('Keep-Alive: 300');
  httpclient.AddExtraHeader('Connection: keep-alive');
  httpclient.AddExtraHeader('Content-Length: 111'); // <-XML length
  httpclient.AddExtraHeader('Content-Type: application/xml; charset=utf-8');
  httpclient.AddExtraHeader(LineEnding);
  httpclient.AddExtraHeader(
    '<?xml version="1.0"?>' + LineEnding +
    '<login>'+ LineEnding +
      'dibo'+ LineEnding +
    '</login>'
  );
  httpclient.SendRequest;

LHTTPClient is a very good socket but lacks some basic features visible for programmer (and PUT, DELETE http methods).
But this is not the end of my problems :P . There is demo how to download file, but there is no how to upload :) . Maybe I'll find a way ...

Regards

xenblaise

  • Sr. Member
  • ****
  • Posts: 342
Re: [SOLVED] LNET HTTPClient - how to POST XML
« Reply #2 on: October 26, 2010, 07:56:49 am »
You'll find a way dibo, your good. :D
This post is helpfull too.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads