Question

Multiple custom webhook requests - JSON formatting

  • 20 January 2022
  • 1 reply
  • 320 views

Hi, all - 

I’m working on a Notion integration for a client that requires me to pass a complex API GET call, modify the return payload, and then pass that modified result into a PATCH call. (I’m trying to collect the body of one Notion page, then append it to another).

Initially, I thought about doing one Custom Request for the GET, a step to modify the return payload, then another Custom Request to PATCH. However, I need the solution to be scalable as the size/length of what the GET returns will vary significantly.

I settled on trying to do the entire thing via a Custom Code (Python). Here’s what I have so far:

source_id = input_data['source_id']
destination_id = input_data['destination_id']
headers = {
'Notion-Version': "2021-08-16",
'Authorization': "REDACTED",
'Content-Type': 'application/json'
}

get = requests.get(f"https://api.notion.com/v1/blocks/{source_id}/children", headers=headers).json()

new_payload = {'children': get['results']}

patch = requests.patch(f"https://api.notion.com/v1/blocks/{destination_id}/children", headers=headers, data=new_payload)

output = patch.text

Problem is, when I modify get by replacing the results key with children (which I need to do in order for the subsequent PATCH to work), new_payload becomes a Python dictionary, which is not JSON serializable. Therefore, when I run the code, patch returns an error 400: Error parsing JSON body.

Outside of Zapier, the solution would be to import json and use json.dumps() to convert new_payload into the correct format, but I understand that isn’t an option within a Zapier code block. Is there any other way to convert a Python dictionary into a JSON serializable variable that I can then put in another request?

I also tried simply passing new_payload into the output, and then doing a new Custom Request action using Data Pass-Through, but I run into the same “Error parsing JSON body” issue.

Thanks in advance!


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Userlevel 7
Badge +11

Hey @a.mitchell,

Instead of using Webhooks by Zapier or a Code by Zapier step, I’m thinking it might be better to build out a private integration for this. It should give you a lot more flexibility in what/how data requested and returned. You can find out more about how to do that here: Zapier Platform - How to Build 

Hopefully that helps! :)