* * *

Author Topic: [Solved] Sending Binary data using TFPHTTPServer and POST  (Read 933 times)

Amir61

  • New member
  • *
  • Posts: 17
    • http://Amir.Aavani.net
[Solved] Sending Binary data using TFPHTTPServer and POST
« on: June 16, 2018, 10:31:46 pm »
Hi all,

  I am trying to send some binary data using TFPHTTPServer and POST. Here is the snippet of my code:
Code: Pascal  [Select]
  1. procedure TMyServer.HandleRequest(var ARequest: TFPHTTPConnectionRequest;
  2.   var AResponse: TFPHTTPConnectionResponse);
  3. ...
  4. begin
  5.     Request := TRequestMessage.Create(AReqeust)
  6.     Response := TResponseMessage.Create;
  7.  
  8.     Self.ServeRequest(ARequest, Request, Response);
  9.  
  10.     BinData := '';
  11.     Response.SaveAsBinary(BinData);
  12.  
  13.     AResponse.ContentType := 'application/octet-stream';
  14.     AResponse.ContentLength:= Length(BinData);
  15.     AResponse.Content := BinData;
  16.    ...
  17.  
  18. end;
  19.  
  20.  
I noticed that the data I send and receive are different. After some more debugging, I figured out that the content of my "BinData" variable and AResponse.Content are different:

For example, if BinData is #10#13#78#111, the value stored in AResponse.Content would be #10#10#78#111.

I wonder if the issue is because of the ContentType? What ContentType should I use?
 
« Last Edit: June 17, 2018, 01:44:58 am by Amir61 »

Amir61

  • New member
  • *
  • Posts: 17
    • http://Amir.Aavani.net
Re: Sending Binary data using TFPHTTPServer and POST
« Reply #1 on: June 17, 2018, 01:44:39 am »
Looks like the following works:

Code: Pascal  [Select]
  1. procedure TMyServer.HandleRequest(var ARequest: TFPHTTPConnectionRequest;
  2.   var AResponse: TFPHTTPConnectionResponse);
  3. ...
  4. begin
  5.     Request := TRequestMessage.Create(AReqeust)
  6.     Response := TResponseMessage.Create;
  7.  
  8.     Self.ServeRequest(ARequest, Request, Response);
  9.  
  10.     BinData := '';
  11.     Response.SaveAsBinary(BinData);
  12.  
  13.     AResponse.ContentType := 'text/html';
  14.     OutputStream := TStringStream.Create(BinData);
  15.     AResponse.ContentStreeam := OutputStream;
  16.  
  17.    ...
  18.  
  19. end;
  20.  
  21.  

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus