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: 16
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: 143
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: 2872
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: 16
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: 16
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: 2872
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: 16
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: 16
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: 2872
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.

Post Reply