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

Convert this to a snap? How?

robert_parks
New Contributor III

Iโ€™m trying to use an API to post data to an application.

There is no account. Instead I have a specific account.

The curl API looks like this:
curl https://communicate.modolabs.net/api/v1/directories/revisions --header โ€œAuthorization: Token myTokenโ€ --form โ€œname=Modo Testโ€ --form โ€œupload=@directory.xmlโ€ where โ€œmyTokenโ€ would be the token I was given.

How do I convert this to the REST Snap?

13 REPLIES 13

mbowen
Employee
Employee

Hi Bob:
I will follow-up tomorrow (Wed, 5/19) with an example of how to do this.

Hi Bob:

Iโ€™m still working on the example. Itโ€™s a little trickier than I thought. Iโ€™ve been inspecting and reconciling request payloads. I am closer to matching a curl request against an echo server (shown below). Contents of directory.xml is just arbitrary xml. Hope to have example finished soon. Thanks for your patience.

POST / HTTP/1.1
Authorization: myToken
Accept: */*
User-Agent: curl/7.68.0
Host: 127.0.0.1:3000
Content-Length: 387
Content-Type: multipart/form-data; boundary=------------------------5217129846a80aae

--------------------------5217129846a80aae
Content-Disposition: form-data; name="name"

Modo Test
--------------------------5217129846a80aae
Content-Disposition: form-data; name="upload"; filename="directory.xml"
Content-Type: application/xml

<directory name="foo">
  <file>file1.txt</file>
  <file>file2.json</file>
</directory>

--------------------------5217129846a80aae--

Hi Bob:

Disclaimer: Iโ€™m familiar with many of our snaps, but actually havenโ€™t done much with our REST snaps. So, maybe others in the Community will chip in with extra explanation or solutions.

Here is a solution that may work. The pipeline is just a single REST POST snap. Youโ€™ll see that the REST snap inserts a few extra attributes that Curl doesnโ€™t which may be ok (ex: Content-Transfer-Encoding: binary).

rest-snap-top

rest-snap-multipart-form-data

File: value1.txt

  Mondo Test

File: directory.xml

  <directory name="foo">
    <file>file1.txt</file>
    <file>file2.json</file>
  </directory>

My Service URL points to a local echo server. Youโ€™ll see that I encoded the โ€œnameโ€ property value into a file โ€“ not super pretty, but gets us close to the representation that we want. I uploaded the file (sldb://), but file could also be referenced on file system (file://).

For our โ€œnameโ€ property, I put an โ€œxโ€ for the filename to indicate that really donโ€™t care about this. If left blank, it will be substituted with the name of the uploaded file (ie, โ€œvalue1.txtโ€).

A couple other observations. Upload Body Type is Multipart FORM-DATA. HTTP entity fields are empty. These fields are ignored for multipart form-data.

Here is the generated request payload.

Accept=[*/*]
Connection=[Keep-Alive]
Host=[127.0.0.1:8001]
Transfer-encoding=[chunked]
Authorization=[Token <my-token>]
Content-type=[multipart/form-data; boundary=Kz659krJ927tbvVX9g5Wwe9siMHfcZOUEqsZvKqr]

--Kz659krJ927tbvVX9g5Wwe9siMHfcZOUEqsZvKqr
Content-Disposition: form-data; name="name"; filename="x"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: binary

Mondo Test

--Kz659krJ927tbvVX9g5Wwe9siMHfcZOUEqsZvKqr
Content-Disposition: form-data; name="upload"; filename="directory.xml"
Content-Type: application/xml; charset=UTF-8
Content-Transfer-Encoding: binary

<directory name="foo">
  <file>file1.txt</file>
  <file>file2.json</file>
</directory>

--Kz659krJ927tbvVX9g5Wwe9siMHfcZOUEqsZvKqr--

Iโ€™m not sure if filename=โ€œxโ€ will cause a problem with submission, but itโ€™s not obvious how to not display this.

Another approach is to submit request as a Multipart RELATED type. This didnโ€™t get me any closer, but thought I would document what I did anyway.

form-related-pipeline

mapper

rest-snap-top

rest-snap-multi-part-related

This pipeline includes a Mapper and REST Snap. I defined the form property in json under an โ€œentityโ€ key in a Mapper, and then referenced this variable in the HTTP Entity field of the REST snap.

In the generated request, this part is encoded as json content with a hard-coded โ€œmetaDataโ€ key. This key name is part of the Multipart RELATED plumbing and is not obvious how to override.

Here is the generated request payload for Multipart RELATED.

Accept=[*/*]
Connection=[Keep-Alive]
Host=[127.0.0.1:8001]
Transfer-encoding=[chunked]
Authorization=[Token <my-token>]
Content-type=[multipart/related; boundary=2_cNQDNeLIqanSgxKib8BNkTrwuamXl]

--2_cNQDNeLIqanSgxKib8BNkTrwuamXl
Content-Disposition: form-data; name="metaData"
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: 8bit

{ "name": "Modo Test" }
--2_cNQDNeLIqanSgxKib8BNkTrwuamXl
Content-Disposition: form-data; name="upload"; filename="directory.xml"
Content-Type: application/xml; charset=UTF-8
Content-Transfer-Encoding: binary

<directory name="foo">
  <file>file1.txt</file>
  <file>file2.json</file>
</directory>

--2_cNQDNeLIqanSgxKib8BNkTrwuamXl--

@mbowen: Did you get a solution? I have a similar requirement where I need to post multiple files to SnapLogic, the number of files and the content type.