How to pass a file with node POST (using with a RESTful API)

I was hopeing some RESTful node (in particular POST) experts can help me out here:

When using Postman to execute a POST to an API, everything works fine -
I am using a token for authentication in the header, a json string in the body. Also in the body, I upload a file (which is then used by the system I access). Postman offers a file-select dialogbox, there is not "string" one can view to see how it translates (at least I can't find anything of that sort).

Now if I want to do this within Knime I am not sure how to pass the file.
A test passing a JSON string for a case where I don't require a file works fine. But for cases when I want to refer to a file - I am not sure how.
In Postman it looks like there are two body portions, whereas the Knime node only has one body portion.

Any suggestions? 
(PS: I am not an expert in RESTful or JSON, I usually "make it work" though.)

 

This is complicated... The request still has just one body but consists of different parts. These types of request require a special encoding of the request body (e.g. multipart/form-data). Technically you can construct the correct encoding with a couple of pre-processing nodes but I suspect this gets quite complicated. Maybe someone from the forum takes this challenge and shares the results.

We have a node that takes several columns and creates an encoded request body on our list, however the list is long...

We have a "Multipart" node in Palladian (warning: usage it not quite straight forward ... feel free to get back in the Palladian sub forum). But you'll have to use the HttpRetriever then, instead of the REST nodes.

-- Philipp

1 Like

Thank you for your responses. 

@thor - which node do you have in mind for such a construction (several columns->encoded request body)? Or did you mean by "on our list" that it is under construction?

@qqilihq - I'll have a look at Palladian, but, yes, I remember from a past scenario that it isn't straightforward..

You could do it with the Java Snippet, for example.

Oh, ok. 

But I assume you don't mean simple string concatenations, one wouldn't need the Java for that.

Some form of conversion into binary perhaps?

 

 

So with the help of qqilihq and the Palladium nodes, I managed to get this to work (well, qqilihq did, I only did copy/paste in the end).

https://www.knime.com/forum/palladian-selenium/post-in-http-retriever-including-file-upload-multiple-body-content

 

Maybe this could be a feature request for the regular Knime POST nodes, the ability to work with multipart bodies?

 

2 Likes

Hi all,

I was wondering if there is any update on this topic. I just tried to upload a file to a third party REST api using the post node. I would either use the body to enter somehow the local path of the file I want to send or I use the binary code of the file as body. Both versions did not work for me and maybe there is a solution in place nowadays using the POST node.

Best, Lars

Hi Lars,

This one is still on list. Add +1 to it :wink:

Br,
Ivan

Hi, is there any new development on my issue?

Best,
Lars

Hello @laval,

nop. As soon as there is progress someone will update this topic. (Internal reference: AP-6694)

Br,
Ivan

1 Like

hello all,

are there any updates relative to supporting multipart/form-data in the POST Request node ?

Best,
Simon

+1. I too wish to upload files (specifically I want to use the Smartsheet API).

While I’ve seen the Palladian workflow demo that uses Postman, I can’t figure out how to supply the Authorization/Bearer that Smartsheet needs.

You can simply supply an Authorization header with the needed token – therefore create a column in the input table called Authorizationm which contaisn a string value such as Bearer XXXX and select this in the Headers tab within the configuration. Voilà!

If you still have trouble, don’t hesitate to get back with a sample workflow!

Cheers,
Philipp

I thought I had done what you had suggested, but still no luck.

I took this: https://www.knime.com/sites/default/files/Palladian_HttpRetriever_Multipart_Example.knwf and turned it into this
Smartsheet_Palladian_HttpRetriever_Multipart_Error.knwf (865.1 KB)

I altered the top-left Table Creator to change the url to the one used by Smartsheet API. This follows the format listed in Smartsheet - Official API and SDK Documentation

POST https://api.smartsheet.com/2.0/sheets/4509093797881732/attachments
Authorization: Bearer ll352u9jujauoqz4gstvsae05

I added a Table Creator (node 31) and cross-joined it to add it to the row. This contains a string as follows:

Authorization
Bearer 7QqredactedB

Here’s what it looks like, and you can see the url, and the Authorization string in the Spec. Note the Node returned an error when ran.

Here I config’d the HTTP Retriever to include the Authorization string in the Headers tab.

The error shows as 400 in the console.

A 400 Bad Request error happens **when a server cannot understand a request that’s been made of it

I have been successful in using the API for POSTing rows to the spreadsheet using the POST Request node, but I don’t think that supports file uploads (if it does, let me know!)

Thanks in advance!

AJ

I’d guess that the authentication is fine and properly set up (otherwise you’d get a different error code), but the data you send is not structured properly.

I noticed that you kept the JSON data from the example workflow, which is just there for figurative purposes. You’ll likely need to adapt it to the Smartsheet API so that it contains the data you want to send with the request. (see the “Table Creator” node with the comment “Define some data to submit as JSON format”).

Hope this helps!

–Philipp

Good catch! It did help a little. I had neglected that node.

Since I didn’t need it, I removed the cross-join that connects it in, and removed the JSON from the 2nd line of the Multipart Entity Creator’s “input column”, but that didn’t help.

I still got a 400 error without this additional JSON.

Perhaps I’ll revisit this at a later date when I have a little more experience under my belt. Thanks again!
AJ

Welcome. Feel free to get back if needed :slight_smile:

–Philipp