【问题标题】:Convert curl POST statement with JSON payload to Python request将带有 JSON 有效负载的 curl POST 语句转换为 Python 请求
【发布时间】:2019-10-08 22:00:36
【问题描述】:

我可以用 curl 运行这个 curl 语句并且运行良好。我已经阅读了很多帖子,但没有任何效果。

curl -X POST "http://some.website.com" -H "accept: application/json" -H "authorization: Basic authcode" -H "Content-Type: application/json" -d "{ \"Fields\": [ \"string\" ], \"Filters\": [ { \"Field\": \"Item\", \"Operator\": \"=\", \"Value\": \"119001\" } ], \"PageSize\": 0, \"PageNumber\": 0}"

到目前为止尝试过的代码

import requests

session = requests.Session()

url = 'http://some.website.com'

headers = {'accept': 'application/json', 'authorization': 'Basic authcode', 'Content-Type': 'application/json'}

data = {'Fields': 'string', 'Filters': { 'Field': 'Item', 'Operator': '=', 'Value': '119001' }, 'PageSize': 0, 'PageNumber': 0}

response = session.post(url, headers=headers, data=data)

print(response.status_code)
print(response.json())

错误 = 无效的 JSON 值

我也试过

import simplejson as json
# ...
# ...
response = session.post(url, headers=headers, data=json.dumps(data))
# ...
# ...

失败 = 检测 JSON 字段时出错

我认为这与嵌套的dict语句有关

【问题讨论】:

  • 您能否在此处添加完整的错误日志。这将有助于发现问题。
  • 你可以在curl.trillworks.com上转换它
  • 如果您必须发送 json,那么您可以使用 post( ..., json=data) 而不是 post(..., data=json.dumps(data))。它应该自动添加标题'Content-Type': 'application/json'

标签: python api curl post request


【解决方案1】:

使用https://httpbin.org/post 我可以看到服务器上接收到了哪些数据(标头和正文),我看到curl 的结果相同

curl -X POST "http://httpbin.org/post" -H "accept: application/json" -H "authorization: Basic authcode" -H "Content-Type: application/json" -d "{\"Fields\": [\"string\"], \"Filters\": [{\"Field\": \"Item\", \"Operator\": \"=\", \"Value\": \"119001\"}], \"PageSize\": 0, \"PageNumber\": 0}"

# result 

{
  "args": {}, 
  "data": "{\"Fields\": [\"string\"], \"Filters\": [{\"Field\": \"Item\", \"Operator\": \"=\", \"Value\": \"119001\"}], \"PageSize\": 0, \"PageNumber\": 0}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "application/json", 
    "Authorization": "Basic authcode", 
    "Content-Length": "122", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.58.0"
  }, 
  "json": {
    "Fields": [
      "string"
    ], 
    "Filters": [
      {
        "Field": "Item", 
        "Operator": "=", 
        "Value": "119001"
      }
    ], 
    "PageNumber": 0, 
    "PageSize": 0
  }, 
  "origin": "83.23.32.69, 83.23.32.69", 
  "url": "https://httpbin.org/post"
}

和Python(使用json=data 或data=json.dumps(data) 代替data=data)

import requests

headers = {
    'Accept': 'application/json',
    'Authorization': 'Basic authcode',
#    'Content-Type': 'application/json',
#    'User-Agent': 'Mozilla/5.0',
}

data = {
  "Fields": [ "string" ],
  "Filters": [ { "Field": "Item", "Operator": "=", "Value": "119001" } ],
  "PageSize": 0,
  "PageNumber": 0
}

response = requests.post('https://httpbin.org/post', headers=headers, json=data)
print(response.text)

# result

{
  "args": {}, 
  "data": "{\"Fields\": [\"string\"], \"Filters\": [{\"Field\": \"Item\", \"Operator\": \"=\", \"Value\": \"119001\"}], \"PageSize\": 0, \"PageNumber\": 0}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "application/json", 
    "Accept-Encoding": "gzip, deflate", 
    "Authorization": "Basic authcode", 
    "Content-Length": "122", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.22.0"
  }, 
  "json": {
    "Fields": [
      "string"
    ], 
    "Filters": [
      {
        "Field": "Item", 
        "Operator": "=", 
        "Value": "119001"
      }
    ], 
    "PageNumber": 0, 
    "PageSize": 0
  }, 
  "origin": "83.23.32.69, 83.23.32.69", 
  "url": "https://httpbin.org/post"
}

只有标题不同:"User-Agent": "curl/7.58.0" 和 "User-Agent": "python-requests/2.22.0"。而Python使用"Accept-Encoding": "gzip, deflate"。


顺便说一句:您可以使用门户curl.trillworks.com 将curl 转换为Python 代码

【讨论】:

    猜你喜欢
    • 2023-01-10
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    相关资源
    最近更新 更多