cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

Sending Multipart POST with attached File

Chrischhan
12-Amethyst

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:

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?

5 REPLIES 5
slangley
23-Emerald II
(To:Chrischhan)

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

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.

slangley
23-Emerald II
(To:Chrischhan)

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

Hello, 

 

i have tried the GitBackup extension you have mentioned and i have the same issues as in one of the first versions (many jears ago....)

 

Starting with import the extension, which works

 

Creating a new repository: Error
After following the instructions I tried to set up the repository via the interface (+ button at the top)
enter my credentials, configure the repo (submitter, url, etc...) after click on "Add" the modal window closes and nothing happens. I did not find any log messages.
After several attempts I tried to create the repo via the composer (GIT.Utility.Thing) which worked directly -> this is now also displayed in the mashup.

 

exporting the entities to push them
select project in export dialog, check dependents and select "export entities"
in the following dialog select all entities, add them (unfortunately only the entities of the project itself) and export -> no feedback -> pressed several times -> it seems it was exported at some point, definitely not after the first time

 

Manually exporting the SourceControlEntities to "GitRepository"-> GitFolder -> Pushing works

 

 

So actually it is a backup solution until we switch to an other development process

Hi @Chrischhan ,

 

The GitBackup extension is not supported by PTC (as we support any product we sell).

In order to help you diagnose what happened, can you please open a new issue here?

After going through many situations like yours, I am 100% sure we can solve what happens there (in the Application Log or Script Log, there should be an error message if the AddRepository does not create the Thing).

Let's chat there.

Top Tags