nerdexam
Cisco

200-901 · Question #509

Refer to the exhibit. A developer creates a Python script to query a REST API by using an HTTP GET request. The remote server will authorize the request only if it includes HTTP Basic Auth…

The correct answer is A. requests.get(url, auth=('admin', 'devnet123')). The Python requests library accepts a plain tuple of (username, password) as the auth argument to automatically encode and send HTTP Basic Auth credentials.

Understanding and Using APIs

Question

Refer to the exhibit. A developer creates a Python script to query a REST API by using an HTTP GET request. The remote server will authorize the request only if it includes HTTP Basic Auth parameters. The username is admin and the password is devnet123. Which line of code needs to be placed on the snippet where the code is missing? A. B. C. D.

Exhibit

200-901 question #509 exhibit

Options

  • Arequests.get(url, auth=('admin', 'devnet123'))
  • Brequests.get(url, auth=HTTPDigestAuth('admin', 'devnet123'))
  • Crequests.get(url, auth=HTTPBasicAuth('admin': 'devnet123'))
  • Drequests.get(url, auth='admin', 'devnet123')

How the community answered

(34 responses)
  • A
    74% (25)
  • B
    15% (5)
  • C
    3% (1)
  • D
    9% (3)

Why each option

The Python requests library accepts a plain tuple of (username, password) as the auth argument to automatically encode and send HTTP Basic Auth credentials.

Arequests.get(url, auth=('admin', 'devnet123'))Correct

Passing auth=('admin', 'devnet123') to requests.get() is the shorthand supported by the requests library for HTTP Basic Authentication; it base64-encodes the credentials and adds the correct Authorization header automatically. This is documented as equivalent to using HTTPBasicAuth('admin', 'devnet123') with proper comma-separated arguments. No additional import is required for this tuple form.

Brequests.get(url, auth=HTTPDigestAuth('admin', 'devnet123'))

HTTPDigestAuth implements the HTTP Digest authentication scheme, which uses a challenge-response mechanism - it does not satisfy a Basic Auth requirement.

Crequests.get(url, auth=HTTPBasicAuth('admin': 'devnet123'))

HTTPBasicAuth('admin': 'devnet123') uses a colon inside the parentheses, which is Python dictionary key syntax and causes a SyntaxError; the correct delimiter between positional arguments is a comma.

Drequests.get(url, auth='admin', 'devnet123')

The auth parameter accepts a single value (a tuple or auth object); passing 'admin' and 'devnet123' as two separate positional arguments to requests.get() is a SyntaxError and will not execute.

Concept tested: Python requests library HTTP Basic Auth usage

Source: https://requests.readthedocs.io/en/latest/user/authentication/#basic-authentication

Topics

#Python#requests library#REST API#HTTP Basic Auth

Community Discussion

No community discussion yet for this question.

Full 200-901 Practice