Zapier代码模块Python输入_数据问题

2024-09-29 03:41:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用zapier电子邮件解析器中的字段创建一个api post请求,并使用zapier代码Python模块将它们插入到请求中

问题是我似乎无法将输入数据字段输入到请求中。在我尝试插入这些字段之前,请求一直正常工作

例如,“firstName”字段有一个名为“first”的输入_数据字段,当我尝试将其插入到数据中时,它会将其打断,我尝试了几种不同的变体括号、括号等

我没有很多python知识,所以我很确定这是一个语法问题。希望有人能给我指出正确的方向。这里是代码与“Api密钥等删除”谢谢

 import requests


'Api-Key': 'hidden',
'Cache-Control': 'no-cache',
'Content-Type': 'application/json', 

    }

    data = '{\n  "arrivalDate": "01/19/2021",  "departureDate": "01/25/2021",  "channelId": hidden,    "arrivalTime": "16:00",  "departureTime": "10:00",  "firstName": input_data("first"), "lastName": "Musetn",  "email": "test@gmail.com",  "phone": "0177123456789",  "notice": "Breakfast, one dog",  "adults": 1,  "priceStatus": 1,  "depositStatus": 1,  "language": "en"}'

    response = requests.post('https://login.smoobu.com/api/apartment/hidden/booking', headers=headers, data=data)

Tags: 代码comapidata电子邮件firstnamepostrequests
1条回答
网友
1楼 · 发布于 2024-09-29 03:41:56

就像前面提到的注释者一样,您应该让python处理将数据转换为json的问题。请尝试以下方法:

import requests
import json


headers = {
    "Api-Key": "hidden",
    "Cache-Control": "no-cache",
    "Content-Type": "application/json",
}

data = {
    "arrivalDate": "01/19/2021",
    "departureDate": "01/25/2021",
    "channelId": hidden,
    "arrivalTime": "16:00",
    "departureTime": "10:00",
    "firstName": input_data("first"),
    "lastName": "Musetn",
    "email": "test@gmail.com",
    "phone": "0177123456789",
    "notice": "Breakfast, one dog",
    "adults": 1,
    "priceStatus": 1,
    "depositStatus": 1,
    "language": "en",
}

response = requests.post(
    "https://login.smoobu.com/api/apartment/hidden/booking",
    headers=headers, 
    data=json.dumps(data)
)

相关问题 更多 >