【问题标题】:{"Error":"Invalid JSON syntax at offset 2"} - receiving this error while trying to get everflow report{"Error":"Invalid JSON syntax at offset 2"} - 在尝试获取 everflow 报告时收到此错误
【发布时间】:2020-09-22 04:43:25
【问题描述】:

我需要从营销平台 Everflow 获得一份报告。

代码背后的想法非常简单——我发出一个 POST 请求:

url = "https://api.eflow.team/v1/affiliates/reporting/daily"
headers = {'x-eflow-api-key': '*MY*API*KEY', 'Content-Type': 'application/json',}
payload = '{ "from": "2020-09-01", "to": "2020-09-10", "timezone_id": 67, "currency_id": "USD"}'

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

print(response.text)

我得到的结果是:

{"Error":"Invalid JSON syntax at offset 2"}

据我了解,问题可能与有效载荷格式有关,但经过两天的尝试,到目前为止还没有成功。

感谢您的帮助!

【问题讨论】:

  • 这是 Python 代码吗?
  • 在这段代码 sn-p 中,payload 被定义但不是payload2,即data=payload2。你确定是对的吗?
  • @Azeem 是的,那是 Python3 代码。抱歉,我没有先修复 payload2 - 只是试图重新格式化该字符串 - 但我在正确的有效负载上也遇到了这个错误
  • 对。请将 Python 和 Python3 标签也添加到您的问题中。另外,添加完整的代码,以便可以单独测试它,即MCVE
  • 您是否尝试直接从their api docu 获取代码sn-p。有一个code snippet 选项卡可以为您生成python 中的代码

标签: python json python-3.x offset


【解决方案1】:

你能试试下面的代码吗?

import requests
import json
url = "https://api.eflow.team/v1/affiliates/reporting/daily"
headers = {"x-eflow-api-key": "*MY*API*KEY", "Content-type": "application/json"}
payload = {'from': '2020-09-01', 'to': '2020-09-10', 'timezone_id': 67, 'currency_id': 'USD'}
payload = json.dumps(payload)
response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

但是,从请求 2.4.2 开始,支持“json”参数。无需指定“Content-Type”(大多数情况下)。

requests.post('http://httpbin.org/post', json={'test': 'foo'})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-03
    • 2022-06-24
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 2015-06-10
    相关资源
    最近更新 更多