Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly requests.put in Python 3.7

I'm trying to communicate with datadog's api using the PUT method but failing with a "400" response. I've looked into the documentation and I'm confident my headers are set up correctly and access keys have been specified. Below is the function I'm working with:

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

Below is what the header is made of:

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

Other articles I've seen so far don't seem to answer my question.

like image 698
Blank Avatar asked Aug 14 '26 07:08

Blank


1 Answers

If you take a look at the API documentation, you will notice it requires you to send required data into the body of the requests.

Here is an example on how to send data into the body using the requests module:

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

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

You will of course need to fill in all the information.

Another example straight from the documentation:

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)

More information in the API documentation and the picture below:

required headers image

like image 59
creed Avatar answered Aug 16 '26 22:08

creed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!