【问题标题】:Sendgrid/python - json could not be unmarshalledSendgrid/python - 无法解组 json
【发布时间】:2020-06-02 07:31:58
【问题描述】:

此python3代码返回错误:

import json
import requests

# Perform a Single Send 

headers = {
    "authorization": "Bearer <my-API-key>",
    "content-type": "application/json"
}

data = {
    "name": "Test Blast",
    "send_to": {
        "segment_ids": ["<a-segment-id>"],
        "all": False
    },
    "email_config": {
        "subject": "Test Blast",
        "html_content": "<h1>My Message</h1><p>Is a very simple one.</p>",
        "generate_plain_content": True
    }
}

url = "https://api.sendgrid.com/v3/marketing/singlesends"
res = requests.post(url, headers=headers, data=json.dumps(payload))
print((res.status_code, res.text))

反应是

(400, '{"errors":[{"field":"name","message":"cannot be empty string"}]}')

这并不过分提供信息。使用https://sendgrid.com/docs/api-reference/ 上的“试用”界面运行相同的数据(将 python 布尔值更改为 true/false) 给出错误响应

{
    "errors": [
        {
            "field": "",
            "message": "json could not be unmarshalled"
        }
    ]

有什么线索可以说明什么是错的,什么是缺失的?

【问题讨论】:

    标签: sendgrid sendgrid-api-v3


    【解决方案1】:

    这行有问题:

    res = requests.post(url, headers=headers, data=json.dumps(payload))
    

    您在请求中传递了空的有效负载变量。您已将 json 分配给数据变量并传递有效负载。

    所以你需要将变量payload更改为这一行中的数据。之后它就可以工作了。

    res = requests.post(url, headers=headers, data=json.dumps(data))
    

    【讨论】:

    • 如果您觉得答案有帮助,请点赞并接受答案
    猜你喜欢
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2018-03-24
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    相关资源
    最近更新 更多