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

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
byronrocha
Posts: 9
Joined: Fri 20 Jul 2018 16:45

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

Post by byronrocha » Fri 20 Dec 2019 23:11

Hi

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

tcaduto12068
Posts: 132
Joined: Wed 17 Aug 2016 05:57

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

Post by tcaduto12068 » Mon 23 Dec 2019 13:55

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.

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

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

Post by ViktorV » Tue 24 Dec 2019 13:00

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.

byronrocha
Posts: 9
Joined: Fri 20 Jul 2018 16:45

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

Post by byronrocha » Thu 02 Jan 2020 17:09

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
Last edited by byronrocha on Thu 02 Jan 2020 17:38, edited 1 time in total.

byronrocha
Posts: 9
Joined: Fri 20 Jul 2018 16:45

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

Post by byronrocha » Thu 02 Jan 2020 17:15

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.

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

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

Post by ViktorV » Fri 03 Jan 2020 09:19

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.

byronrocha
Posts: 9
Joined: Fri 20 Jul 2018 16:45

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

Post by byronrocha » Fri 03 Jan 2020 16:52

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.

byronrocha
Posts: 9
Joined: Fri 20 Jul 2018 16:45

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

Post by byronrocha » Fri 03 Jan 2020 16:55

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');

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

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

Post by ViktorV » Wed 08 Jan 2020 14:39

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.

GoSane
Posts: 4
Joined: Fri 15 Aug 2014 10:46

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

Post by GoSane » Sat 05 Feb 2022 16:18

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;

YanishevskiyVI
Devart Team
Posts: 70
Joined: Wed 02 Jun 2021 09:30

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

Post by YanishevskiyVI » Mon 07 Feb 2022 10:41

Hi there!
Thanks for your interest in our products!
Your suggestions are highly appreciated.

Post Reply