【问题标题】:Python equivalent for Curl giving 400 ResponseCurl 的 Python 等效项给出 400 响应
【发布时间】:2020-10-27 21:35:44
【问题描述】:

正在尝试使用 requests 模块将 shell curl 重写为 python 脚本...

我的 shell 脚本:

#!/bin/bash

ip=$1
url=https://xxx.xx.xx.com
secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
expand=computerStatus


curl --silent -X POST "$url/api/computers/search?expand=$expand" -H "Content-Type: application/json" -H "api-secret-key: $secret" -H "api-version: v1" -d '{"maxItems": 10,"searchCriteria": [{"fieldName": "hostName","stringTest": "equal", "stringValue": "'"$ip"'"}]}' -k > $file

以上在 bash 中运行良好。

我正在尝试转换为类似的 python 等效项

我尝试了什么

import json
import requests
import sys

ip = sys.argv[1]

sys_info = []

url=https://xxx.xx.xx.com
secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
expand=computerStatus

headers = {
    'Content-Type': 'application/json',
    'api-secret-key': secret,
    'api-version': 'v1',
}

params = (
    ('expand', 'expand'),
)

data = '{"maxItems": 10,"searchCriteria": [{"fieldName": "hostName","stringTest": "equal", "stringValue": ip}]}'

response = requests.post('https://url/api/computers/search?expand=expand', headers=headers, params=params, data=data)

print(response)

<Response [400]>

我收到 400 响应.. 不确定我在语法中遗漏了哪里...

【问题讨论】:

    标签: python api curl python-requests http-status-code-400


    【解决方案1】:

    数据需要是字典,试试这个:

    data = {
        "maxItems": 10, 
        "searchCriteria": [{"fieldName": "hostName","stringTest": "equal", "stringValue": ip}]
        }
    

    还将参数转换为字典:

     params = {"expand" : expand}
    

    发帖时:

    response = requests.post(f'https://{url}/api/computers/search', headers=headers, params=params, data=data)
    
    ### or alternatively you can
    
    response = requests.post(f'https://{url}/api/computers/search?expand={expand}', headers=headers, data=data)
    

    【讨论】:

    • 嗨,我还是得到 400,你能确认我的 expand=computerStatusresponse = requests.post('https://url/api/computers/search?expand=expand', headers=headers, params=params, data=data) 在语法上是否正确吗?特别是上面的网址中的expand=expand
    • 哦,对了,如果要在字符串中对其进行编码,则此处不需要参数,基本上使用 f 字符串(假设您使用的是 python 3.6+ 或使用格式化字符串),@987654326 @我会相应地编辑我的答案
    • 谢谢,还是得到400,这次我直接包含了url而不是替换,但结果相同.... shell脚本工作正常..有点奇怪
    • 这很可能意味着无法解析主机名。就像它说的那样。而不是 url/api 可能尝试 localhost:8000/api 或 127.0.0.1:8000/api 假设端点托管在您的计算机使用端口 8000。哦,也许您正试图将 url 写成字符串,在这种情况下,请使用我用于 {expand} 的 f 字符串,即:response = requests.post(f'https://{url}/api/computers/search?expand={expand}', headers=headers, data=data) 我再次相应地编辑了我的答案
    • 是的,我现在得到的似乎是网络问题requests.exceptions.ConnectionError: HTTPSConnectionPool(host='https', port=443): Max retries exceeded with url: /xxx.xx..x..com/api/computers/search?expand=computerStatus (Caused by NewConnectionError('&lt;urllib3.connection.VerifiedHTTPSConnection object at 0x0DCFDB30&gt;: Failed to establish a new connection: [Errno 11001] getaddrinfo failed')) ...但我想知道为什么当我从 linux 机器运行 linux 脚本时它会成功..hmm..谢谢您耐心地帮助我在这个
    猜你喜欢
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 2011-01-10
    • 2011-10-12
    • 2010-09-12
    • 1970-01-01
    相关资源
    最近更新 更多