【问题标题】:Django: convert cURL to python requestsDjango:将 cURL 转换为 python 请求
【发布时间】:2020-02-19 00:33:39
【问题描述】:

我有 cURL 代码,我需要使用 requests 库将其转换为 python 代码。我想在 django 后端软件中使用它。 我尝试了几种方法,但我遇到了一些错误。有人可以帮我弄这个吗? 这是 cURL 代码:

    curl -XPOST -H 'cache-control: no-cache' -H 'content-type: application/json' -H
'X-Client-Id: asdf1234' -H 'X-Client-Secret: qwer9876' -d '{
"planId":"BASIC", "planName":"Basic subscription plan", "amount":12,
"intervalType":"week", "intervals":2,"description":"This is the standard plan
for our services"}' 'https://test.cashfree.com/api/v2/subscription-plans'

【问题讨论】:

标签: python python-requests


【解决方案1】:

将来,您可以使用方便的转换器,例如this

import requests

headers = {
    'cache-control': 'no-cache',
    'content-type': 'application/json',
    'X-Client-Id': 'asdf1234',
    'X-Client-Secret': 'qwer9876',
}

data = '{"planId":"BASIC", "planName":"Basic subscription plan", "amount":12,"intervalType":"week", "intervals":2,"description":"This is the standard planfor our services"}'

response = requests.post('https://test.cashfree.com/api/v2/subscription-plans', headers=headers, data=data)

【讨论】:

  • 我试过这个。当我打印 response.text 时,我收到此消息 {"status":"ERROR","subCode":"400","message":"Invalid JSON in request body"}
  • 这可能是因为你没有删除\n。它为我返回 {"status":"ERROR","subCode":"401","message":"Authentication Failure"}。 @sreekanthkura7
  • 我确实删除了\n。您收到该错误消息是因为我在此处提到的客户端 ID 和密码不正确。
  • 是的,我知道。该错误不是因为我的代码。 @sreekanthkura7 。你应该准确地复制我的答案,它会起作用的。
猜你喜欢
  • 1970-01-01
  • 2016-01-30
  • 2020-03-18
  • 2023-04-02
  • 2019-08-17
  • 2021-07-08
相关资源
最近更新 更多