Python HTTP请求替换bash-cu

2024-09-29 09:27:35 发布

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

我将一些自动化脚本从bash移植到python,它们几乎都是以下格式的curl命令:

curl -k -H "Content-Type: application/json" -X POST -d '{               "Request": {
                          "MessageID": "xxxx",
                          "MessageDateTime": "xxxxx",
                          "SourceSystem": "xxxx",

           }
}' https://myUrl.xxx

在Python中准确地构造它的最佳方法是什么?到目前为止,我已经:

^{pr2}$

我希望确保-k、-d和-x bash选项在该脚本中得到反映。谢谢您!在


Tags: 命令脚本bashjsonapplicationrequest格式type
1条回答
网友
1楼 · 发布于 2024-09-29 09:27:35

您可以直接使用requests.post-k对应于verify=False

from datetime import datetime as DateTime
import requests
import json

URL = "https://myUrl.xxx"

message = {
    "Request": {
        "MessageID": "xxxx",
        "MessageDateTime": DateTime.now().isoformat(),
        "SourceSystem": "xxxx",
    }
}

response = requests.post(URL, data=json.dumps(message), verify=False, headers={"Content-Type":"application/json"})
data = response.json()

相关问题 更多 >