DevNet Associate (DEVASC) v1.0Understanding and Using APIsMedium
A network automation engineer is developing a Python script to interact with a Cisco Meraki API. The API documentation specifies that all requests must include a custom header named 'X-Meraki-API-Key' with the API key value. Which of the following `requests` library snippets correctly adds this custom header to a GET request?
- Arequests.get(url, params={'X-Meraki-API-Key': 'YOUR_API_KEY'})
- Brequests.get(url, auth=('X-Meraki-API-Key', 'YOUR_API_KEY'))
- Crequests.get(url, data={'X-Meraki-API-Key': 'YOUR_API_KEY'})
- Drequests.get(url, headers={'X-Meraki-API-Key': 'YOUR_API_KEY'})
Show answer & explanationAnswer & explanation
Correct answer: D. requests.get(url, headers={'X-Meraki-API-Key': 'YOUR_API_KEY'})
The `requests` library uses the `headers` parameter, which accepts a dictionary, to send custom HTTP headers with a request. This is the correct way to include the 'X-Meraki-API-Key'.
Why the other options are wrong
- A. The `params` parameter is used for URL query parameters, not HTTP headers.
- B. The `auth` parameter is for HTTP authentication schemes like Basic or Digest, not for custom API key headers.
- C. The `data` parameter is for sending request body data, typically in POST/PUT requests, not headers.
Python requests custom headers
In the Python `requests` library, custom HTTP headers are sent using the `headers` parameter, which takes a dictionary of header names and values.
- Uses `headers` parameter.
- Parameter expects a dictionary.
- Essential for custom API keys or specific content types.
Memory trick: Requests: Headers are for 'head' information.