简体   繁体   English

将参数传递给 rest api - python 的正确方法

[英]Correct way to pass parameters to rest api - python

I have a REST API documentation on page 97, that gives me the rest api snippet like this below:我在第 97 页上有一个REST API 文档,它给了我 rest Z8A5DA52ED1064417D359 片段如下: 在此处输入图像描述

I did exactly the same with python.我对 python 做了同样的事情。 See below:见下文:

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)

But I am getting a TypeError: unhashable type: 'list' in line "op": "=" .但我得到一个TypeError: unhashable type: 'list' in line "op": "=" I did checkout all the unhashable type list posts and tried changing the list to a tuple.我确实检查了所有不可散列的类型列表帖子,并尝试将列表更改为元组。 But then the API throws 400 bad request.但随后 API 抛出 400 bad request。 Is this a problem with the API or am i doing something wrong?这是 API 的问题还是我做错了什么?

{} contains the dict not list. {} 包含字典而不是列表。 you should try removing {} in filters您应该尝试删除过滤器中的 {}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM