如何使用Python请求从RESTAPI查询JSON格式的数据?

如何使用Python请求从RESTAPI查询JSON格式的数据?,json,python-3.x,python-requests,Json,Python 3.x,Python Requests,我试图使用Sophos UTM RESTful API将一些配置从一个设备复制到另一个设备。API实际上是虚张声势的,因此这不应该特定于Sophos设备 脚本的第一部分向源设备查询特定节点: import requests import json from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureReq

我试图使用Sophos UTM RESTful API将一些配置从一个设备复制到另一个设备。API实际上是虚张声势的,因此这不应该特定于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并成功查询数据。返回的
状态\u 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)
未返回任何错误,但它不起作用,print语句返回以下内容:

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,请求提供了一个
json
实用方法,您可以使用它来检索数据:
update=update.json()


有关更多信息,请参见

谢谢您的回答。这和我正在做的事情(payload=repsonse.json())不一样吗?很抱歉,我明白你的意思了。
update
的内容是
[]
,所以基本上是空的。猜猜为什么会这样?我会看看那个端点(
/api/http/exception
)是什么,以确保您的参数、头和负载是正确的,因为返回一个空列表可能是这个特定端点的有效响应。好的,我想我已经找到了为什么它不能按我希望的方式工作。对源设备的请求返回一个嵌套字典。我不确定API是否接受这一点,或者它的格式是否不正确。我将编辑我的原始帖子,这样你就可以看到返回的内容。也许我们可以找到一种格式化它的方法,以便它被API接受。再次感谢!