Upload a file using PUT and ContentType = 'multipart/form-data'
-
- Posts: 9
- Joined: Fri 20 Jul 2018 16:45
Upload a file using PUT and ContentType = 'multipart/form-data'
Hi
Is there any way to use SecureBridge components to Upload a file using a PUT method and with ContentType 'multipart/form-data' ?
Is there any way to use SecureBridge components to Upload a file using a PUT method and with ContentType 'multipart/form-data' ?
-
- Posts: 132
- Joined: Wed 17 Aug 2016 05:57
Re: Upload a file using PUT and ContentType = 'multipart/form-data'
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.
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'
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: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.
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;
-
- Posts: 9
- Joined: Fri 20 Jul 2018 16:45
Re: Upload a file using PUT and ContentType = 'multipart/form-data'
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
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.
-
- Posts: 9
- Joined: Fri 20 Jul 2018 16:45
Re: Upload a file using PUT and ContentType = 'multipart/form-data'
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'
To upload a file to the site using the PUT method, you can use the following code:
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.
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;
-
- Posts: 9
- Joined: Fri 20 Jul 2018 16:45
Re: Upload a file using PUT and ContentType = 'multipart/form-data'
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.
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.
-
- Posts: 9
- Joined: Fri 20 Jul 2018 16:45
Re: Upload a file using PUT and ContentType = 'multipart/form-data'
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');
Request.Headers.Add('x-key','ABCDEFG');
Re: Upload a file using PUT and ContentType = 'multipart/form-data'
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.
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'
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.
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;
-
- Devart Team
- Posts: 70
- Joined: Wed 02 Jun 2021 09:30
Re: Upload a file using PUT and ContentType = 'multipart/form-data'
Hi there!
Thanks for your interest in our products!
Your suggestions are highly appreciated.
Thanks for your interest in our products!
Your suggestions are highly appreciated.