如何在 Python 3.7 中正确 requests.put
How to correctly requests.put in Python 3.7
我正在尝试使用 PUT 方法与 datadog 的 api 通信,但失败并返回“400”响应。我查看了文档,确信我的 header 设置正确并且访问密钥已指定。下面是我正在使用的函数:
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
下面是 header 的构成:
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
和下图中:
我正在尝试使用 PUT 方法与 datadog 的 api 通信,但失败并返回“400”响应。我查看了文档,确信我的 header 设置正确并且访问密钥已指定。下面是我正在使用的函数:
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
下面是 header 的构成:
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
和下图中: