Page 1 of 1

Upload a file using PUT and ContentType = 'multipart/form-data'

Posted: Fri 20 Dec 2019 23:11
by byronrocha
Hi

Is there any way to use SecureBridge components to Upload a file using a PUT method and with ContentType 'multipart/form-data' ?

Re: Upload a file using PUT and ContentType = 'multipart/form-data'

Posted: Mon 23 Dec 2019 13:55
by tcaduto12068
I don't know but you can use the Indy10 HTTP client to do that .
I have not used the devart HTTP component and it's fairly new from what I remember.

When using the HTTP components you don't do a "put" you do a POST. Uploading a file with HTTP is completely different than SFTP and if you are don't mean using HTTP for the upload there is no need to worry about multippart/form-data with SFTP as all uploads and downloads are binary.

if you need up simulate uploading multiple files at once like you can do with multipart/form-data just zip them and then unzip them on the server side.

I am not sure what you are really asking but you could also use the indy components and just save a multipart file and then then upload it via SFTP and then decode it on the SFTP server if you really wanted to, but if your not using HTTP that would be kind of a waste of time to do over SFTP.

Re: Upload a file using PUT and ContentType = 'multipart/form-data'

Posted: Tue 24 Dec 2019 13:00
by ViktorV
To work with data via the HTTP (HTTPS) protocol, you can use the TScHttpWebRequest component: https://devart.com/sbridge/docs/index.h ... equest.htm
HTTP is not designed specifically for transferring files, however, if your host supports this functionality through PUT request method, you can do it the following way:

Code: Select all

var
  Request: TScHttpWebRequest;
  Response: TScHttpWebResponse;
  ResponseStr: string;
  Buf: TBytes;
begin
  Request := TScHttpWebRequest.Create(URL);
  try
    Request.Method := rmPut;
    Buf := TEncoding.UTF8.GetBytes('This is a test that posts this string to a Web server.');

    Request.ContentType := 'multipart/form-data';
    Request.ContentLength := Length(Buf);
    Request.WriteBuffer(Buf);
    Response := Request.GetResponse;
    Request.Free;
  finally
    Request.Free;
  end;
end;
Our components are more stable and performant than Indy components. In addition, they don't rely on any third-party libraries to work with the HTTPS protocol.

Re: Upload a file using PUT and ContentType = 'multipart/form-data'

Posted: Thu 02 Jan 2020 17:09
by byronrocha
Hi

Thanks.
Also, how could I send the Raw Request starting like this...

----boundary.ipw.202008.3131111111.0
Content-Type: application/pdf
Content-Disposition: form-data; name="FormFile"; filename="Document1.pdf"
Content-Transfer-Encoding: binary

%PDF-1.5

Re: Upload a file using PUT and ContentType = 'multipart/form-data'

Posted: Thu 02 Jan 2020 17:15
by byronrocha
And, yes. we are not the owners of the Host, from the client side we need to upload a file using PUT method to a URL and also specifying the file name and others security parameters.

Re: Upload a file using PUT and ContentType = 'multipart/form-data'

Posted: Fri 03 Jan 2020 09:19
by ViktorV
To upload a file to the site using the PUT method, you can use the following code:

Code: Select all

var
  Request: TScHttpWebRequest;
  Response: TScHttpWebResponse;
  ResponseStr: string;
  Stream: TFileStream;
begin
  Request := TScHttpWebRequest.Create(URL);
  Stream := TFileStream.Create(FileName, fmOpenRead);
  try
    Request.Method := rmPut;

    Request.ContentType := 'application/pdf';
    Request.TransferEncoding := 'binary';
    Request.Headers.Add('Content-Disposition', 'form-data; name="FormFile"; filename="Document1.pdf"');

    Request.ContentLength := Stream.Size;
    Request.SendChunked := True;
    Request.RequestStream := Stream;

    Response := Request.GetResponse;
    ResponseStr := Response.ReadAsString;
    Response.Free;
  finally
    Stream.Free;
    Request.Free;
  end;
end;
Please write in more detail what you mean by the phrase "others security parameters". If you want to work with the file as in the file system, then, as we wrote earlier, HTTP is not designed for this.

Re: Upload a file using PUT and ContentType = 'multipart/form-data'

Posted: Fri 03 Jan 2020 16:52
by byronrocha
Thanks for the Info.

First I will upgrade my securebridge component because there are new properties I can't find like SendChunked.
My current version is 9.0.2

I will try this after the upgrade.

Re: Upload a file using PUT and ContentType = 'multipart/form-data'

Posted: Fri 03 Jan 2020 16:55
by byronrocha
And regarding "other security parameters", we need to pass an API Key, so I added a line like this:

Request.Headers.Add('x-key','ABCDEFG');

Re: Upload a file using PUT and ContentType = 'multipart/form-data'

Posted: Wed 08 Jan 2020 14:39
by ViktorV
Thank you for interest to our product.
The use of the TScHttpWebRequest.Headers property to set "other security parameters" is a correct way to solve the defined task.
Feel free to contact us if you have any further questions about our products.

Re: Upload a file using PUT and ContentType = 'multipart/form-data'

Posted: Sat 05 Feb 2022 16:18
by GoSane
And here is a fully working code, adding the boundary stuff. Maybe there's a cleaner way to do it.
If filename contains non-ASCII (accented) characters, you have to encode it in quoted-printable format.

Code: Select all

procedure SendFile(idrealm,idannuaire:Integer; Filename:String);
Const APIendpoint = '/file/upload';
      Bearer      = 'eyJhbGciOiJIUzIiFMdxCX0PdvHIETXBKKYFtYDu_sPSiY1ZNLeUJg681s';
      APIurl      = 'https://rooturl.yourdomain.tld';
      APIport     = 8088;
      boundary    = '----------BOUNDARY'; // Feel free to add some randomness here
var
  Request: TScHttpWebRequest;
  Response: TScHttpWebResponse;
  ResponseStr: string;
  Stream: TFileStream;
  FinalStream: TMemoryStream;
  url: String;
  s:AnsiString;
begin
  url:=Format('%s:%d%s',[APIurl,APIport,APIendpoint])+Format('?realm_id=%d&idannuaire=%d',[idrealm,idannuaire]);
  Request := TScHttpWebRequest.Create(URL);
  Stream := TFileStream.Create(FileName, fmOpenRead);
  FinalStream:=TMemoryStream.Create;
  try
    Request.Method := rmPut;
    Request.ContentType := 'multipart/form-data; boundary='+boundary;
    Request.TransferEncoding := 'binary';
    Request.headers.Add('Authorization','Bearer '+Bearer);
    Request.KeepAlive:=True;

    s := '--'+boundary+^M+^J
        +'Content-Disposition: form-data; name="file"; filename="'+FileName+'"'+^M+^J
        +'Content-Type: */*'+^M+^J
        +^M+^J;
    FinalStream.Write(s[1],Length(s));
    FinalStream.CopyFrom(Stream,Stream.Size);
    s := ^M+^J+
         '--'+boundary+'--'+^M+^J;
    FinalStream.Write(s[1],Length(s));
    FinalStream.Position:=0;

    Request.ContentLength := FinalStream.Size;
    Request.SendChunked := True;
    Request.RequestStream := FinalStream;

    Response := Request.GetResponse;
    ResponseStr := Response.ReadAsString;
    ShowMessage(ResponseStr);
    Response.Free;
  finally
    FinalStream.Free;
    Stream.Free;
    Request.Free;
  end;
end;

Re: Upload a file using PUT and ContentType = 'multipart/form-data'

Posted: Mon 07 Feb 2022 10:41
by YanishevskiyVI
Hi there!
Thanks for your interest in our products!
Your suggestions are highly appreciated.