Sending Multipart POST with attached File
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:
{
"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:
{
"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?

