繁体   English   中英

使用 requests / http.client 在 python 中运行 curl 调用

[英]run curl call in python using requests / http.client

我正在尝试在 python 中执行 curl 调用。

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" --header "Authorization:xxxx" -d "{
\"ProductNames\": [
\"PReport\"
],
\"SearchType\": \"FullAddress\",
\"FullAddress\": \"123 Main Street, New York, 10001\",
\"ReferenceId\": \"\"
}" "https://api.aws.com:443/api/rep/grep"

我努力了:

 import http.client 
 import urllib.parse 

 addr = '123 Main Street, New York, 10001'
 prod = 'PReport'
 stype = 'FullAddress'
 

 conn = http.client.HTTPSConnection("https://api.aws.com:443/api/rep/grep") 

    headers = { 
        'Accept': "application/json", 
        'Authorization': "xxxx", 
        } 

    address = urllib.parse.quote(addr)

    url = "https://api.aws.com:443/api/rep/grep?ProductName={}&SearchType={}&FullAddress={}".format(prod, stype, address)

    conn.request("GET", url, headers=headers)

    res = conn.getresponse() 

在招摇,我只看到: https://api.aws.com:443/api/rep/grep ,而不是完整的请求网址。

如何使用上面的信息在 python 中巧妙地进行 curl 调用?

当你说你想执行那个 curl 调用时,我假设它可以工作并且你想用 Python 发出同样的请求。

您的 Python 代码可能存在更多问题,但我立即发现一个我认为不正确的问题是您使用 URL 查询参数,而 curl 命令使用-d ,这是--data的缩写形式。 以下是相关文档:

- 数据

(HTTP MQTT) 将 POST 请求中的指定数据发送到 HTTP 服务器,就像浏览器在用户填写 HTML 表单并按下提交按钮时所做的一样。 这将导致 curl 使用内容类型 application/x-www-form-urlencoded 将数据传递给服务器。 与 -F、--form 进行比较。

[...]

https://curl.se/docs/manpage.html

这意味着它进入请求正文。 但是,在您的 Python 代码中,您将它们作为查询字符串参数放置,这似乎并不等效。 但是您还说您“看不到完整的请求网址”,这让我有点困惑,因为您不应该看到除“https://api.aws.com:443/api/rep/grep”之外的其他网址" 卷曲,或者。 一种快速查看使用 curl 得到什么的方法是使用工具“netcat”,当您将它安装在系统上时,通常由“nc”调用。

例如,要监听 1234 端口,你可以这样调用 netcat:

nc -l 1234

有关 netcat 的更多信息,请参阅https://linux.die.net/man/1/nc 当然,您还可以使用其他工具。 这只是一个建议。

当使用上面的 curl 命令行对 localhost 端口 1234 发出相同的请求时,它会打印以下内容:

POST /api/rep/grep HTTP/1.1
Host: localhost:1234
User-Agent: curl/7.79.1
Content-Type: application/json
Accept: application/json
Authorization:xxxx
Content-Length: 132

{
"ProductNames": [
"PReport"
],
"SearchType": "FullAddress",
"FullAddress": "123 Main Street, New York, 10001",
"ReferenceId": ""
}

因此,请求参数并不是您认为的 URL 的一部分,并且您可能对您应该在 API 端点的实现代码中看到的内容有错误的期望。

但是,如果 curl 调用仍然有效,那么与您的 curl 调用更等效的 Python 代码可能是这样的:

import requests

url = "https://api.aws.com:443/api/rep/grep"
headers = {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "Authorization": "xxxx"
}
data = """{
    "ProductNames": [ "PReport" ],
    "SearchType": "FullAddress",
    "FullAddress": "123 Main Street, New York, 10001",
    "ReferenceId": ""
}"""

resp = requests.post(url, headers=headers, data=data)
print(resp.status_code)

此代码在以相同方式针对 netcat 侦听的端口运行时,会显示一个与上述 curl 调用相匹配的请求。

在最新版本中还有一个json参数,可以通过使用字典来发送数据,从而使代码更清晰:

data = {
    "ProductNames": [ "PReport" ],
    "SearchType": "FullAddress",
    "FullAddress": "123 Main Street, New York, 10001",
    "ReferenceId": ""
}
requests.post(url, headers=headers, json=data)

可以在https://requests.readthedocs.io/en/latest/下找到最新版本的 requests Python 模块的文档。

此外,在您的问题中转换各种执行 HTTP 请求的方式的任务非常普遍,以至于有很多在线工具可以自动执行此操作。 一个例子是https://curlconverter.com/ 该网站还包含其他相关信息,但我没有仔细检查您的查询结果。 它看起来确实产生了相同的代码。 不过,在过度依赖自动化工具之前,最好花点时间了解常见 HTTP 请求的各个部分是如何传输的。

暂无
暂无

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

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