POST with dynamic body

Hello everyone :slight_smile:
I am relatively new at Knime and I’m delighted with this powerful low-code tool.
But I’m stuck in a step :frowning:
I get some data using the GET request → convert JSON to Table → Manipulated the Table → Combined a column and now I would like to get the content from the row and use at post.
The endpoint documentation needs a POST Body with this schema:

{
“visitor”: {
“name”: “Livechat Visitor”,
“email”: “visitor@Knime”,
“token”: “iNKE8a6k6cjbqWhWd”,
“phone”: “55 51 5555-5555”,
“customFields”: [{
“key”: “address”,
“value”: “Knime street”,
“overwrite”: true
}]
}
}

So, how can I put this dynamic content at the POST Body? I tried to use the String Manipulator but I’m failing to make this happen.

I tought it would be something like this:

{
“visitor”: {
“name”: “$name$”,
“email”: “$email$”,
“token”: “$token$”,
“phone”: “$phone$”,
“customFields”: [{
“key”: “address”,
“value”: “$address$”,
“overwrite”: true
}]
}
}

But didn’t work :sweat_smile:

Thanks everyone!

Hi @gbrfilipe and welcome to the Knime Community.

Yes, this won’t work. You can use some placeholders instead, and then have them replaced by the dynamic values.

Also, if you have multiple POSTs to do, you should do them in batches with a delay (wait) between the batches.

You can take a look at this thread for more details:

Here’s another example:

2 Likes

Hi @gbrfilipe

To supplement @bruno29a’s answer, you have to be careful here since the POST schema has a nested array in it for the customFields. Due to this I would recommend the Columns to JSON node.

Here:
I start with creating the customFields array by filtering for key, value and overwrite in the Columns to JSON node. Use the unnamed root option.


Note: I opt to remove the source columns at the very bottom, it’s depends on your actual use case if you want to keep them.

Next, convert it to an array via either Create Collection Column or GroupBy.

Repeat the same exercise as before by creating a JSON for the remaining columns plus the one created earlier for the customFields. Use visitor as root key name this time.

It results in the following JSON:

{
  "visitor" : {
    "name" : "Livechat Visitor",
    "email" : "visitor@Knime",
    "token" : "iNKE8a6k6cjbqWhWd",
    "phone" : "55 51 5555-5555",
    "customFields" : [ {
      "key" : "address",
      "value" : "Knime street",
      "overwrite" : "true"
    } ]
  }
}

You can then pass this on towards the POST request.

Re-iterating what Bruno also said, keep the fair use policy of the API in mind as in creating a loop with delays between each request to prevent that they will boot you out :wink:

WF:
POST with dynamic body.knwf (23.9 KB)

Hope this helps!

3 Likes