Python:POST正文中的变量应该作为对象提供,而不是递归JSON编码的字符串

hec6srdp  于 2023-06-25  发布在  Python
关注(0)|答案(2)|浏览(125)

我使用以下方法将变量插入POST请求体:
def get_variables(date): return '{{"date":"{0}","timezone":"Europe"}}'.format(date)
查询如下:
query Exchange($date: String!, $timezone: String) { getIntervals(date: $date, timezone: $timezone) { date intervals { start end description type code } } }
我在执行get_variable时收到这个错误消息
variables in a POST body should be provided as an object, not a recursively JSON-encoded string.
有什么想法如何将它转换成Python中的JSON对象吗?

46scxncf

46scxncf1#

不要将此数据编码为字符串,而是返回一个常规字典:

def get_variables(date):
    return {"date": str(date), "timezone": "Europe"}
41zrol4v

41zrol4v2#

更新:已解决。
我使用json.loads()函数进行反序列化-将字符串转换为对象的行为。--> def get_variables(date): return json.loads('{{"date": "{0}", "timezone": "Europe"}}'.format(date))
谢谢大家。

相关问题