200-901 exam dumps

200-901 practice question 53 of 204

Cisco DevNet Associate. Associate level, Cisco. Free question with the correct answer and a full explanation.

200-901 Question 53

Single answer

You are building a Python script to fetch data from a REST API using the requests library. The API endpoint requires an HTTP GET request and includes an API key for authentication in the headers. Which of the following Python code snippets correctly performs this task?

  1. A

    import requests response = requests.get('https://api.example.com/data', headers={'Authorization': 'Bearer API_KEY'}) print(response.json())

  2. B

    import requests response = requests.post('https://api.example.com/data', headers={'Authorization': 'Bearer API_KEY'}) print(response.json())

  3. C

    import requests response = requests.get('https://api.example.com/data', headers={'API_KEY': 'Bearer API_KEY'}) print(response.json())

  4. D

    import requests response = requests.get('https://api.example.com/data', params={'Authorization': 'Bearer API_KEY'}) print(response.json())

Show answer and explanation

Correct answer: A

Explanation

To perform an HTTP GET request using the requests library, the requests.get() method must be used. The API key is typically included in the Authorization header in the format Bearer API_KEY for authentication. Any deviation from these requirements, such as using the wrong HTTP method or placing the API key in the wrong location, will result in an incorrect implementation.

  • A. Correct.

    This option is correct because it uses the requests.get() method to perform an HTTP GET request and correctly includes the API key in the Authorization header.

  • B. Incorrect.

    This option is incorrect because it uses the requests.post() method instead of the requests.get() method to make the request. The API endpoint specifically requires an HTTP GET request.

  • C. Incorrect.

    This option is incorrect because it uses the wrong key (API_KEY) in the headers. The correct header key for including an API key in this scenario is Authorization.

  • D. Incorrect.

    This option is incorrect because it places the API key in the params argument instead of the headers argument. The params argument is used for query string parameters, not for authentication headers.

Timed practice exam

Take a 200-901 practice test under exam conditions

75 questions in 120 minutes, drawn from this bank, with a score report and a per-question review when you finish.

Start timed exam