向RESTAPI python传递参数的正确方法

2024-06-16 10:36:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在第97页有一个REST API documentation,它给了我下面这样的rest api片段: enter image description here

我对python也做了同样的事情。见下文:

import requests

base_url = 'https://hostname/api'
session = requests.Session()
headers = {
    'Content-Type' : 'application/json',
    'cache-control': 'no-cache',
    'x-endeavour-sessionid': '23423423werfer23f23rf2'
}

_params = {
    "resourceType": "vm",
    "from": "hlo",
    "filter": {
        [
            {
                "property": "storageProfileName",
                "value":    "testname",
                "op":       "="
            }
        ]
    }
}

_data = f'''{{
    "name": "*",
    "hypervisorType": "vmware"
}}'''

url_unprotectedvm = base_url + '/hypervisor/search'
unprotectedvm_data = session.post(url_unprotectedvm, params=_params, data=_data, headers=headers, verify=False)
print(unprotectedvm_data)

但是我在第"op": "="行中得到了一个TypeError: unhashable type: 'list'。我签出了所有未损坏的类型列表帖子,并尝试将列表更改为元组。但随后API抛出400个错误请求。这是API的问题还是我做错了什么


Tags: restapiurlcache列表databasesession
1条回答
网友
1楼 · 发布于 2024-06-16 10:36:23

{}包含dict not列表。您应该尝试删除筛选器中的{}

_params = {
    "resourceType": "vm",
    "from": "hlo",
    "filter":
        [
            {
                "property": "storageProfileName",
                "value":    "testname",
                "op":       "="
            }
        ]
    
}

相关问题 更多 >