Lazarus

Programming => Networking and Web Programming => Topic started by: billyb123 on February 14, 2018, 03:34:40 am

Title: how to do a file upload with synapse to a url?
Post by: billyb123 on February 14, 2018, 03:34:40 am
so I need to upload a file using synapse. I also need to add certain headers to my http post request.
It seems my brain has melted,  I know about HttpPostFile, but I cant add headers to that method?
Basically, my question is (1) how to add headers to HttpPostFile method? or (2) is there a synapse way to simply add file upload to httpmethod method?

Sorry if its a silly question, its 3AM here, my brain has stopped working hours ago.


Code: [Select]
{$mode delphi}

uses sysutils, strutils, httpsend, classes, ssl_openssl, synautil;

var
  http: thttpsend;

begin
  http := thttpsend.create;
  http.headers.add('SOME AUTH HEADER');
  http.headers.add('ANOTHER AUTH HEADER');
  http.headers.add('Content-Type: application/octet-string');
  (* is there a way to simply "add file to upload" without manually editing http request? *)
  http.free;
end.
Title: Re: how to do a file upload with synapse to a url?
Post by: Thaddy on February 14, 2018, 09:05:26 am
http://synapse.ararat.cz/doc/help/
Look at HttpPostBinary or HttpPostFile
That may be just what you need.
Title: Re: how to do a file upload with synapse to a url?
Post by: Leledumbo on February 15, 2018, 06:16:05 pm
Looking at THTTPSend documentation, it only provides generic Document property, no easy file upload mechanism, you must build the body manually. If you must use Synapse, I don't think you have any other choice.
Title: Re: how to do a file upload with synapse to a url?
Post by: rvk on February 15, 2018, 06:27:42 pm
Looking at THTTPSend documentation, it only provides generic Document property, no easy file upload mechanism, you must build the body manually. If you must use Synapse, I don't think you have any other choice.
Like Thaddy already mentioned, there are functions HttpPostFile() and HttpPostBinary() which already do that work. So there is no need to build the body manually.

The only thing is, if you want to add headers you need to copy that function and do the adding yourself. But that's just a copy/paste and adding a line like:
Code: Pascal  [Select][+][-]
  1. HTTP.Headers.Add('Header: content');

For example, this is the HttpPostFile of Synapse of which you can make a copy:
Code: Pascal  [Select][+][-]
  1. function HttpPostFile(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 + 'Content-Type: Application/octet-string' + CRLF + CRLF;
  14.     WriteStrToStream(HTTP.Document, s);
  15.     HTTP.Document.CopyFrom(Data, 0);
  16.     s := CRLF + '--' + Bound + '--' + CRLF;
  17.     WriteStrToStream(HTTP.Document, s);
  18.     HTTP.MimeType := 'multipart/form-data; boundary=' + Bound;
  19.     HTTP.Headers.Add('Header: content');
  20.     Result := HTTP.HTTPMethod('POST', URL);
  21.     if Result then
  22.       ResultData.LoadFromStream(HTTP.Document);
  23.   finally
  24.     HTTP.Free;
  25.   end;
  26. end;
So adding a header in there shouldn't be that difficult (I highlighted the line).
Title: Re: how to do a file upload with synapse to a url?
Post by: billyb123 on February 19, 2018, 05:41:06 pm
thanks guys
TinyPortal © 2005-2018