gpt4 book ai didi

python - 如何在 Python 3.7 中正确地 requests.put

转载 作者:行者123 更新时间:2023-12-05 02:03:06 25 4
gpt4 key购买 nike

我正在尝试使用 PUT 方法与 datadog 的 api 通信,但失败并返回“400”响应。我查看了文档,我确信我的 header 设置正确并且访问 key 已指定。以下是我正在使用的功能:

def editMonitor(monitor_Data):

api_url = 'https://api.datadoghq.com/api/v1/monitor/' + str(monitor_Data['id'])

response = requests.put(api_url, monitor_Data, headers=headers)

print(response)

if response.status_code == 200:
return json.loads(response.content.decode('utf-8'))
else:
return None

下面是标题的组成部分:

headers = {'Content-Type': 'application/json',
'DD-API-KEY': '**********************',
'DD-APPLICATION-KEY': '***********************'
}

到目前为止我看到的其他文章似乎没有回答我的问题。

最佳答案

如果你看一下 API documentation ,您会注意到它要求您将所需数据发送到请求正文中。

这是一个关于如何使用请求模块将数据发送到正文的示例:

data = {
"message": yourMessageInfo,
"name": yourNameInfo,
"object": yourObjectInfo,
"priority": yourPriorityInfo,
"query": yourQueryInfo,
"tags": yourTagsInfo,
"type": yourTypeInfo
}

response = requests.put(api_url, headers=headers, json=data)

您当然需要填写所有信息。

另一个直接来自文档的例子:

data = {
"message": "string",
"name": "string",
"options": {
"enable_logs_sample": false,
"escalation_message": "string",
"evaluation_delay": "integer",
"include_tags": false,
"locked": false,
"min_failure_duration": "integer",
"min_location_failed": "integer",
"new_host_delay": "integer",
"no_data_timeframe": "integer",
"notify_audit": false,
"notify_no_data": false,
"renotify_interval": "integer",
"require_full_window": false,
"restricted_roles": [],
"silenced": {
"<any-key>": "integer"
},
"synthetics_check_id": "string",
"threshold_windows": {
"recovery_window": "string",
"trigger_window": "string"
},
"thresholds": {
"critical": "number",
"critical_recovery": "number",
"ok": "number",
"unknown": "number",
"warning": "number",
"warning_recovery": "number"
},
"timeout_h": "integer"
},
"priority": "integer",
"query": "string",
"tags": [],
"type": "string"
}

response = requests.put(api_url, headers=headers, json=data)

更多信息在 API documentation和下面的图片:

required headers image

关于python - 如何在 Python 3.7 中正确地 requests.put,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65438638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com