* * *

Author Topic: Sending file by post method (synapse) doesnt work  (Read 748 times)

cpalx

  • Hero Member
  • *****
  • Posts: 560
Sending file by post method (synapse) doesnt work
« on: March 01, 2018, 05:02:28 am »
hello
i try to send a file using synapse to a web server (php),  but $_FILES always is empty

PHP code

Code: PHP  [Select]
  1. <?php
  2. var_dump( $_FILES )
  3. ?>
  4.  


Client code
Code: Pascal  [Select]
  1. function TForm1.MyHttpPostFile(const URL, FieldName, FileName: string;
  2.   const Data: TStream; const ResultData: TStrings): Boolean;
  3. var
  4.   HTTP: THTTPSend;
  5.   Bound, s: string;
  6. begin
  7.   Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary';
  8.   HTTP := THTTPSend.Create;
  9.   try
  10.     s := '--' + Bound + CRLF;
  11.     s := s + ' content-disposition: form-data; name="' + FieldName + '";';
  12.     s := s + ' filename="' + FileName +'"' + CRLF;
  13.     s := s + ' Authorization: ' + ediTOKEN.Text+ CRLF;
  14.     s := s + ' Content-Type: application/json' +  CRLF;
  15.     s := s + ' Content-Type: Application/octet-string' + CRLF + CRLF;
  16.     WriteStrToStream(HTTP.Document, s);
  17.     HTTP.Document.CopyFrom(Data, 0);
  18.     s := CRLF + '--' + Bound + '--' + CRLF;
  19.     WriteStrToStream(HTTP.Document, s);
  20.     HTTP.MimeType := 'multipart/form-data; boundary=' + Bound;
  21.     Result := HTTP.HTTPMethod('POST', URL);
  22.     if Result then
  23.       ResultData.LoadFromStream(HTTP.Document)
  24.     else
  25.       ResultData.Text:= 'Error Code:' + IntToStr(HTTP.ResultCode);
  26.   finally
  27.     HTTP.Free;
  28.   end;
  29. end;  
  30.  
  31.  

Button click event
Code: Pascal  [Select]
  1.  
  2. procedure TForm1.btnSendFileClick(Sender: TObject);
  3. var
  4.    ListAnswer: TStringList;
  5.    FileStream: TFileStream;
  6.    Myfile: Array of string;
  7. begin
  8.    if not oDialog.Execute then
  9.       exit;
  10.  
  11.    ListAnswer:= TStringList.Create;
  12.    FileStream:= TFileStream.Create( oDialog.FileName, fmOpenRead );
  13.    MyHttpPostFile( ediURL.Text,
  14.                  ExtractFileNameWithoutExt( oDialog.FileName ),
  15.                  ExtractFileName( oDialog.FileName ),
  16.                  FileStream,
  17.                  ListAnswer
  18.                  );
  19.    FreeAndNil( FileStream );
  20.  
  21.    memServerAnswer.Text:= ListAnswer.Text;
  22.  
  23. end;                                      
  24.  


and always display

Code: HTML5  [Select]
  1. <br />
  2. array(0) {
  3. }
  4.  
  5.  

Leledumbo

  • Hero Member
  • *****
  • Posts: 7908
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Sending file by post method (synapse) doesnt work
« Reply #1 on: March 01, 2018, 07:10:42 pm »
I'm a bit lazy to analyze, so please display the final HTTP.Document contents before you call HTTPMethod and let's see if it contains valid HTTP header/body for multipart/form-data request.

cpalx

  • Hero Member
  • *****
  • Posts: 560
Re: Sending file by post method (synapse) doesnt work
« Reply #2 on: March 01, 2018, 07:18:09 pm »
Is empty

Leledumbo

  • Hero Member
  • *****
  • Posts: 7908
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Sending file by post method (synapse) doesnt work
« Reply #3 on: March 01, 2018, 07:33:00 pm »
Is empty
So how could you expect the server can get your request? %)
Maybe you just need to:
Code: Pascal  [Select]
  1. HTTP.Document.Seek(0, soBeginning);
before calling HTTPMethod, so the stream is read from the beginning instead of the end, which is of course, empty.

rvk

  • Hero Member
  • *****
  • Posts: 3441
Re: Sending file by post method (synapse) doesnt work
« Reply #4 on: March 01, 2018, 08:28:30 pm »
AFAIK the HTTP.Document.Position is set to 0 prior to sending it in HTTPMethod.

Second, I tried the code and HTTP.Document is not empty (you probably don't know how to read it back from the stream because that's what HTTP.Document is).

The content is this:
Code: [Select]
--463F8555_Synapse_boundary
 content-disposition: form-data; name="C:\Users\Rik\Documents\test"; filename="test.ini"
 Authorization: 123
 Content-Type: application/json
 Content-Type: Application/octet-string

[test_section]
test=test

--463F8555_Synapse_boundary--
I have two big notes on that.
1) Why two Content-Types ???
2) Why the spaces before every line?

If I remove the spaces I get this as result:
Code: [Select]
array(1) {
  ["C:\Users\Rik\Documents\test"]=>
  array(5) {
    ["name"]=>
    string(8) "test.ini"
    ["type"]=>
    string(16) "application/json"
    ["tmp_name"]=>
    string(14) "/tmp/php2Mp7fo"
    ["error"]=>
    int(0)
    ["size"]=>
    int(259)
  }
}

So, remove the spaces in front of all the lines and just use one content-type and it should work.

cpalx

  • Hero Member
  • *****
  • Posts: 560
Re: Sending file by post method (synapse) doesnt work
« Reply #5 on: March 01, 2018, 08:36:39 pm »
@rvk you are right, Thanks a lot

 

Recent

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