【问题标题】:How to use Python Requests to query JSON-formatted data from a REST API?如何使用 Python 请求从 REST API 查询 JSON 格式的数据?
【发布时间】:2019-10-14 12:55:18
【问题描述】:

我正在尝试使用 Sophos UTM RESTful API 将某些配置从一台设备复制到另一台设备。 API 实际上是 Swagger,所以这不应该特定于 Sophos 设备。

脚本的第一部分查询特定节点的源设备:

import requests
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

src_URL = 'https://172.16.2.100:4444/api/objects/http/exception'
src_headers = {
        'Accept': 'application/json',
        'Authorization': 'Basic dG9rZW46U2tCaENIZUlRZnlVeEpBU3dqYUh2c0VHRmZjdWtDTFg='
        }
response = requests.get(src_URL, headers=src_headers, timeout=15, verify=False)

那部分工作正常。该脚本能够连接到 API 并成功查询数据。返回的status_code 是 200。

接下来,脚本尝试使用从源设备返回的数据并将其复制到目标设备:

payload = response.json()

dst_URL = 'https://172.16.2.101:4444/api/objects/http/exception'
dst_headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Authorization': 'Basic dG9rZW46dlZ3WnVZZGxpd01IRkxNVXpKVXZtZXhiZGZHSExobnI='
        }
update =  requests.post(dst_URL, headers=dst_headers, json=payload, timeout=15, verify=False)

result = update.content
print (result)

没有返回错误但它不工作并且打印语句返回这个:

b'[]'

所以基本上,update 的内容似乎是空的,并且由于某种原因是一个字节。

为什么update的内容是空的?我希望它包含 JSON 格式的返回数据。

编辑

好的,我想我知道为什么它没有按预期工作。 API 将嵌套字典返回给第一个设备的查询。

curl -X GET --header 'Accept: application/json' --header 'Authorization: Basic dG9rZW46U2tCaENIZUlRZnlVeEpBU3dqYUh2c0VHRmZjdWtDTFg=' 'https://172.16.2.100:4444/api/objects/http/exception/' -k
[
  {
    "_locked": "",
    "_ref": "REF_FirefoxUpdateException",
    "_type": "http/exception",
    "aaa": [],
    "comment": "Work around Firefox Byte Range-Request Bug",
    "domains": [
      "^http://download\\.cdn\\.mozilla\\.net/.*\\.mar"
    ],
    "endpoints_groups": [],
    "name": "Firefox Update",
    "networks": [],
    "operator": "OR",
    "skiplist": [
      "av",
      "sandbox",
      "extensions",
      "contenttype_blacklist",
      "check_max_download"
    ],
    "sp_categories": [],
    "status": true,
    "tags": [],
    "user_agents": []
  },
  {
    "_locked": "",
    "_ref": "REF_TrendmicroUpdateException",
    "_type": "http/exception",
    "aaa": [],
    "comment": "Work around problems with Trendmicro product update",
    "domains": [
      "^http://([A-Za-z0-9.-]+\\.)?activeupdate\\.trendmicro\\.com(:80)?/"
    ],
    "endpoints_groups": [],
    "name": "Trendmicro Update",
    "networks": [],
    "operator": "OR",
    "skiplist": [
      "cache",
      "av",
      "sandbox",
      "extensions",
      "contenttype_blacklist",
      "url_filter",
      "content_removal"
    ],
    "sp_categories": [],
    "status": true,
    "tags": [],
    "user_agents": []
  },
  {
    "_locked": "",
    "_ref": "REF_AdobeDownloadException",
    "_type": "http/exception",
    "aaa": [],
    "comment": "Allows Adobe Software Update without content scanning side effects.",
    "domains": [
      "^https?://([A-Za-z0-9.-]*\\.)?adobe\\.com/",
      "^https?://([A-Za-z0-9.-]*\\.)?macromedia\\.com/"
    ],
    "endpoints_groups": [],
    "name": "Adobe Software Update",
    "networks": [],
    "operator": "OR",
    "skiplist": [
      "av",
      "sandbox",
      "extensions",
      "content_removal",
      "user_auth",
      "url_filter"
    ],
    "sp_categories": [],
    "status": true,
    "tags": [],
    "user_agents": []
  },
  {
    "_locked": "",
    "_ref": "REF_TeamviewerException",
    "_type": "http/exception",
    "aaa": [],
    "comment": "Work around Teamviewer SSL handshake Bug",
    "domains": [
      "^https?://(?:[A-Za-z0-9-]+\\.)+teamviewer\\.com/?"
    ],
    "endpoints_groups": [],
    "name": "Teamviewer Remote Access",
    "networks": [],
    "operator": "OR",
    "skiplist": [
      "sandbox",
      "ssl_scanning"
    ],
    "sp_categories": [],
    "status": true,
    "tags": [],
    "user_agents": []
  },
  {
    "_locked": "",
    "_ref": "REF_jzpKEIbHnW",
    "_type": "http/exception",
    "aaa": [],
    "comment": "Allows Windows Update without content scanning side effects.",
    "domains": [
      "^https?://([A-Za-z0-9.-]*\\.)?windowsupdate\\.com/",
      "^https?://([A-Za-z0-9.-]*\\.)?microsoft\\.com/"
    ],
    "endpoints_groups": [],
    "name": "Microsoft Windows Update",
    "networks": [],
    "operator": "OR",
    "skiplist": [
      "av",
      "sandbox",
      "extensions",
      "ssl_scanning",
      "check_max_download"
    ],
    "sp_categories": [],
    "status": true,
    "tags": [],
    "user_agents": []
  },
  {
    "_locked": "",
    "_ref": "REF_EppBrokerCommunicationException",
    "_type": "http/exception",
    "aaa": [],
    "comment": "Allow endpoints to connect to Sophos LiveConnect",
    "domains": [
      "^https?://mcs[0-9]*-[A-Za-z0-9]{4}\\.broker\\.sophos\\.com/",
      "^https?://mcs[0-9]*-[A-Za-z0-9]{4}-d\\.broker\\.sophos\\.com/",
      "^https?://[A-Za-z0-9.-]*-wdx-[A-Za-z0-9.-]*\\.broker\\.sophos\\.com/",
      "^https?://[A-Za-z0-9.-]*\\.upe\\.p\\.hmr\\.sophos\\.com/",
      "^https?://[A-Za-z0-9.-]*\\.hydra\\.sophos\\.com/",
      "^https?://[A-Za-z0-9.-]*\\.cwg\\.sophos\\.com/",
      "^https?://[A-Za-z0-9.-]*\\.mojave\\.net/"
    ],
    "endpoints_groups": [],
    "name": "Sophos LiveConnect",
    "networks": [],
    "operator": "OR",
    "skiplist": [
      "user_auth",
      "cache",
      "av",
      "sandbox",
      "extensions",
      "contenttype_blacklist",
      "url_filter",
      "content_removal",
      "ssl_scanning",
      "certcheck",
      "certdate",
      "check_max_download"
    ],
    "sp_categories": [],
    "status": true,
    "tags": [],
    "user_agents": []
  },
  {
    "_locked": "",
    "_ref": "REF_NokiaOviSuiteException",
    "_type": "http/exception",
    "aaa": [],
    "comment": "Work around Nokia Ovi Suite Authentication Bug",
    "domains": [
      "^https?://[A-Za-z0-9.-]*\\.ovi\\.com/",
      "^https?://[A-Za-z0-9.-]*nds[0-9]\\.nokia\\.com/",
      "^https?://beta\\.nokia\\.com/"
    ],
    "endpoints_groups": [],
    "name": "Nokia Ovi Suite/Store",
    "networks": [],
    "operator": "OR",
    "skiplist": [
      "user_auth",
      "av",
      "sandbox"
    ],
    "sp_categories": [],
    "status": true,
    "tags": [],
    "user_agents": []
  },
  {
    "_locked": "",
    "_ref": "REF_SophosServicesException",
    "_type": "http/exception",
    "aaa": [],
    "comment": "Allow Sophos Services",
    "domains": [
      "^https?://[A-Za-z0-9.-]*\\.sophosupd\\.com/",
      "^https?://[A-Za-z0-9.-]*\\.sophosupd\\.net/",
      "^https?://[A-Za-z0-9.-]*\\.sophosxl\\.net/"
    ],
    "endpoints_groups": [],
    "name": "Sophos Services",
    "networks": [],
    "operator": "OR",
    "skiplist": [
      "user_auth",
      "av",
      "sandbox",
      "extensions",
      "contenttype_blacklist",
      "url_filter",
      "content_removal",
      "ssl_scanning",
      "certcheck",
      "certdate",
      "check_max_download"
    ],
    "sp_categories": [],
    "status": true,
    "tags": [],
    "user_agents": []
  },
  {
    "_locked": "",
    "_ref": "REF_ChromeUpdateException",
    "_type": "http/exception",
    "aaa": [],
    "comment": "Work around Chrome Byte Range-Request Bug",
    "domains": [
      "^https?://([A-Za-z0-9.-]+\\.google\\.com\\/)(([A-Za-z0-9.-\\/]+)?)[0-9._]*chrome_(updater|installer)\\.exe"
    ],
    "endpoints_groups": [],
    "name": "Chrome Update",
    "networks": [],
    "operator": "OR",
    "skiplist": [
      "av",
      "sandbox",
      "extensions",
      "contenttype_blacklist",
      "check_max_download"
    ],
    "sp_categories": [],
    "status": true,
    "tags": [],
    "user_agents": []
  },
  {
    "_locked": "",
    "_ref": "REF_iPhoneiPadYoutubeException",
    "_type": "http/exception",
    "aaa": [],
    "comment": "Work around iPad/Iphone Byterange Bug",
    "domains": [
      "^http://[A-Za-z0-9.-]+\\.googlevideo\\.com/videoplayback",
      "^http://[A-Za-z0-9.-]+\\.youtube\\.com/videoplayback"
    ],
    "endpoints_groups": [],
    "name": "iphone/iPad youtube",
    "networks": [],
    "operator": "OR",
    "skiplist": [
      "av",
      "sandbox",
      "contenttype_blacklist",
      "content_removal",
      "check_max_download"
    ],
    "sp_categories": [],
    "status": true,
    "tags": [],
    "user_agents": []
  },
  {
    "_locked": "",
    "_ref": "REF_jzpKEIbHnX",
    "_type": "http/exception",
    "aaa": [],
    "comment": "Allows Apple Update without content scanning side effects.",
    "domains": [
      "^https?://([A-Za-z0-9.-]*\\.)?apple\\.com\\.?/"
    ],
    "endpoints_groups": [],
    "name": "Apple Update",
    "networks": [],
    "operator": "OR",
    "skiplist": [
      "av",
      "sandbox",
      "extensions"
    ],
    "sp_categories": [],
    "status": true,
    "tags": [],
    "user_agents": []
  }

我不确定 API 是否接受使用 POST 的此类数据,或者其格式是否不正确。

将嵌套字典(虽然不知道如何)分割成单个字典并使用for 循环遍历每个字典是否是个好主意?

否则,数据需要以其他方式格式化,但我不知道该怎么做。

【问题讨论】:

    标签: json python-3.x python-requests


    【解决方案1】:

    如果返回的内容是 json,requests 提供了一个json 实用方法,您可以使用它来检索数据:update = update.json()

    更多信息请见Requests docs

    【讨论】:

    • 感谢您的回答。这和我做的不是一回事吗(payload = repsonse.json())?
    • 好的,对不起,我明白你的意思。 update的内容是[],所以基本是空的。猜猜这是为什么?
    • 我会看看那个端点 (/api/http/exception) 以确保你的参数、标头、有效负载是正确的,因为返回一个空列表可能是这个特定端点的有效响应。跨度>
    • 好的,我想我知道为什么它没有按我想要的那样工作。对源设备的请求返回一个嵌套字典。我不确定 API 是否接受它,或者它的格式是否正确。我将编辑我的原始帖子,以便您查看返回的内容。也许我们可以找到一种方法来格式化它,以便它被 API 接受。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2019-03-31
    • 1970-01-01
    相关资源
    最近更新 更多