Skip to main content
13-Aquamarine
May 10, 2022
Question

Sending Multipart POST with attached File

  • May 10, 2022
  • 1 reply
  • 2844 views

Hi,

 

i want to upload a file created by the TWX Platform to an external System. The external api accepts POST requests with multipart/form-data. It requires that the file is attached to the "files" key.

A correct CURL command (that is working) is:

curl -u REDACTED:REDACTED -x POST --url "http://172.18.0.6:1080/archive" -F files=@testarchive.zip

Now i have two options with "ContentLoaderFunctions.PostMultipart" from TWX (that i know so far):

 

Send File from Repository

 

let params = {
 repository: "SystemRepository" /* STRING */,
 url: "http://172.18.0.6:1080/archive" /* STRING */,
 password: "REDACTED" /* STRING */,
 pathOnRepository: "/archive.zip" /* STRING */,
 username: "REDACTED" /* STRING */,
};

// result: JSON
result = Resources["ContentLoaderFunctions"].PostMultipart(params);

 

 

 

This creates a correct Multipart Request but the api requires that the File is attached to a "files" key not the created "file" key.

The intercepted request (https://www.mock-server.com/) has the following data:

Spoiler
{
 "method": "POST",
 "path": "/archive",
 "headers": {
 "content-length": [
 "31665"
 ],
 "User-Agent": [
 "Apache-HttpClient/4.5.13 (Java/11.0.14.1)"
 ],
 "Host": [
 "172.18.0.6:1080"
 ],
 "Content-Type": [
 "multipart/form-data; boundary=iTC1VvylIfQ9NzMkRnK6UPFNzUjrKcqoi63f"
 ],
 "Connection": [
 "Keep-Alive"
 ],
 "Accept-Encoding": [
 "gzip,deflate"
 ]
 },
 "keepAlive": true,
 "secure": false,
 "body": "--iTC1VvylIfQ9NzMkRnK6UPFNzUjrKcqoi63f\r\nContent-Disposition: form-data; name=\"file\"; filename=\"SourceControl_Test.zip\"\r\nContent-Type: application/zip\r\nContent-Transfer-Encoding: binary\r\n\r[STRIPPED]"
 }

Send Custom Multipart

 

const table = Resources["InfoTableFunctions"].CreateInfoTable();
table.AddField({ name: "files", baseType: "BLOB" });
table.AddRow({
 files: Things["SystemRepository"].LoadBinary({ path: "/archive.zip" }),
});

let params = {
 partsToSend: table /* INFOTABLE */,
 url: "http://172.18.0.6:1080/archive" /* STRING */,
 password: "REDACTED" /* STRING */,
 username: "REDACTED" /* STRING */,
};

// result: JSON
result = Resources["ContentLoaderFunctions"].PostMultipart(params);

 

 

 

This creates not a correct Multipart Request for the api (missing "files" field).

The intercepted request (https://www.mock-server.com/) has the following data:

Spoiler
{
 "method": "POST",
 "path": "/archive",
 "headers": {
 "content-length": [
 "42687"
 ],
 "User-Agent": [
 "Apache-HttpClient/4.5.13 (Java/11.0.14.1)"
 ],
 "Host": [
 "172.18.0.6:1080"
 ],
 "Content-Type": [
 "multipart/form-data; boundary=Gp8YexQsfUaP187g4AFJM0pGOoyxCXflEopmd-"
 ],
 "Connection": [
 "Keep-Alive"
 ],
 "Accept-Encoding": [
 "gzip,deflate"
 ]
 },
 "keepAlive": true,
 "secure": false,
 "body": "--Gp8YexQsfUaP187g4AFJM0pGOoyxCXflEopmd-\r\nContent-Disposition: form-data; name=\"files\"\r\nContent-Type: multipart/form-data; charset=ISO-8859-1\r\nContent-Transfer-Encoding: 8bit\r\n\r\nUEsDB[STRIPPED BASE64]"
 }

So is there any way to change the Fieldname in the first approach or to create a correct Multipart in the second case? or is there any other way i can use to upload a file to the external api?

1 reply

Community Manager
May 11, 2022

Hi @Chrischhan 

 

Are you trying to back up your source control from ThingWorx?  If so, take a look at this community post.  It's a little old but you can find the latest gitbackup extension on github.

 

Let us know if this is a viable option for what you are trying to achieve.

 

Regards.

 

--Sharon

13-Aquamarine
May 20, 2022

Hello,

yes I want to move the sources from the TWX server to a backup. However not under source control but as a defined export (save to downloads, next to the repository). In the future, other data packages should also be loaded there for archiving, as well as stopping the source backup (SourceCode administration and development are just emerging). Therefore, this would be a short-term solution, but would "spam" the repository and would not solve the problem of archiving.

Community Manager
June 2, 2022

Hi @Chrischhan

 

What actions are you trying to take on the file?  Are you trying to parse it for some other system?  If you're not trying to manipulate the file, you could use the File Transfer Subsystem for transferring files to another location.  This method would bypass using the API on the external system.  Refer to this link for configuring file transfers.  You could then use the EMS for doing simple file transfers, as well as other file management activities.

 

This post isn't exactly what you're needing  but it might be helpful.

 

What is the external API you're trying to upload to?  Have you checked the documentation for that system?

 

Regards.

 

--Sharon