首页 > 解决方案 > 如何将python dict变成Curl

问题描述

由于某些原因,我对 API 的请求失败了。这是代码:

def create_alert(alert_dict)
    url =  host + '/api/alerts'


    headers = {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer tokengen'
    }
    response = requests.request('POST', url, headers = headers, data=json.dumps(alert_dict))
    return response


alert_dict = {
    "priority": "1",
    "to": to,
    "from":  from_
}


response = create_alert(alert_dict)
print(response)

我这样做的方式是将所有内容保存到字典中,然后用它发出 POST 请求。但由于某种原因,我收到了 500 错误。

这是正在工作的卷曲

curl --location --request POST "http://newhost.com/api/alerts" \
  --header "Content-Type: application/json" \
  --header "Authorization: Bearer tokengen" \
  --data "{

    \"to\": \"sample@email.com\",
    \"from\": \"from@email.com\",
    \"priority\": \"1\",

}"

--data我应该如何将我的 dict 转换为在请求时可以像 curl 一样工作

标签: python

解决方案


推荐阅读