Zapier代码中的Python mulitpart / formdata POST请求

问题描述 投票:2回答:1

我目前正在与Zapier合作以使一些任务自动化,但是我陷入了以下困境:

我正在尝试使用包含文件的Zapier Webhooks发送POST请求。我可以使它通过邮递员工作,因为Debitoor的API(我要发送到的地方)非常清楚。

但是,我无法使其在Zapier Webhooks中运行。我还尝试使用Zapier代码(Python),因为我可以从邮递员查看python代码。但是我对此并不熟悉,可能需要一些帮助才能开始。

1。)首先,这是API参考:https://developers.debitoor.com/api-reference#files

2。)然后,我将Postman与正在运行的这段代码(Python请求)一起使用:

import requests

url = "https://api.debitoor.com/api/files/v1"

querystring = {"token":"eyJ1c2VyIjoiNWE0NmVjYjUxOTE0ODEwMDFjMTkxYzZmIiwiYXBwIjoiNTdiMmZlMDkxZTkwMjQwZjAwNDZhNWEyIiwiY2hhbGxlbmdlIjowLCIkZSI6MCwiJHQiOjE1MjE4NzAwNTQ1OTd9CsKRw5xbw5_DhHUWw5QJw4zDj8KnXsOaeMKA","fileName":"test.pdf"}

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"Bildschirmfoto 2018-04-05 um 09.59.46 1.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'cache-control': "no-cache",
    'postman-token': "716e7723-2dc1-6384-059d-960feb563443"
}

response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)

3。)试图将代码复制到Zapier Code,但我不知道如何实现该文件。在Zapier中,我触发了入站电子邮件以获取附件,然后附件被“水化”。看起来像这样:

hydrate|||.eJwtjMsOwiAUBf_lrosKNFDZu3Xh1hjC46KklTaFxDRN_11qXJ7JzFkhplxMcqijB8U5l7yT5wZCxMHrZN4Iqo4BMzTgXuh63eMCioruLKiobEwFU9FlmXb1WrX-Y-ZnBrX-Qj2NsSpzBfcV_o-3C2GUisPkwx7sj5D5UQpDmeMnwqW1pPWBE-OYJdYwdCJQT9sWtse2fQEK1Tjl:1eqY0S:s2Ek27XO54PVSm9q_mVMDN8o1uY|||hydrate

如何将Python代码连接到水合文件?我没有文件使用经验,找不到任何有用的帮助。希望有人有主意吗?

python webhooks zapier
1个回答
0
投票

我试图将AWS S3文件导入我的API。

事实证明Zapier像这里描述的那样为我的文件添加了水分。

然后我成功地提取了文件的内容,并像这样将其发送到我的API:

import urllib.request

auth_token = input_data['auth_token'] # Authentication token for my API
csv_file = input_data['csv_file'] # The "hydrate|||..." variable: that's my S3 file
file_type = 'text/csv'

fp = urllib.request.urlopen(csv_file)
file_bytes = fp.read() # Binary content of my S3 file
fp.close()

url = 'http://my.api.com/importer/resource'
headers = {
  'accept': 'application/vnd.api-v1+json',
  'authorization': auth_token,
  'user-agent': 'Zapier'
}

files = {'csv_file': ('bulk_resources.csv', file_bytes, file_type, {'Expires': '0'})}

response = requests.post(url, headers=headers, files=files)

return response.json()
© www.soinside.com 2019 - 2024. All rights reserved.