0
0
Rest APIprogramming~10 mins

Basic authentication in Rest API - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add the Authorization header for Basic authentication.

Rest API
headers = {"Authorization": "Basic [1]"}
Drag options to blanks, or click blank then click option'
Ab64encode(b'user:pass').decode('utf-8')
Bbase64.encode('user:pass')
Cbase64.b64decode('user:pass')
Db64encode('user:pass')
Attempts:
3 left
💡 Hint
Common Mistakes
Encoding the string directly without converting to bytes.
Using base64 decode instead of encode.
Not decoding the base64 bytes back to string.
2fill in blank
medium

Complete the code to create the Basic authentication header value correctly.

Rest API
auth_value = "Basic " + [1]
Drag options to blanks, or click blank then click option'
Abase64.b64encode('username:password')
Bbase64.b64encode(b'username:password').decode('utf-8')
Cbase64.b64decode(b'username:password').decode('utf-8')
Dbase64.encode('username:password')
Attempts:
3 left
💡 Hint
Common Mistakes
Encoding a string directly without converting to bytes.
Using decode before encoding.
Using base64 decode instead of encode.
3fill in blank
hard

Fix the error in the code to correctly set the Basic authentication header.

Rest API
import base64
credentials = 'admin:1234'
encoded = base64.b64encode([1])
headers = {'Authorization': 'Basic ' + encoded.decode('utf-8')}
Drag options to blanks, or click blank then click option'
Abase64.b64decode(credentials)
Bcredentials
Cencoded
Dcredentials.encode('utf-8')
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string directly to b64encode causing a TypeError.
Using base64 decode instead of encode.
Trying to encode the already encoded variable.
4fill in blank
hard

Fill both blanks to create a Basic authentication header with username and password.

Rest API
import base64
user = 'user1'
passw = 'pass1'
creds = f"[1]:[2]"
encoded = base64.b64encode(creds.encode('utf-8')).decode('utf-8')
headers = {'Authorization': 'Basic ' + encoded}
Drag options to blanks, or click blank then click option'
Auser
Bpassw
Cusername
Dpassword
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variable names.
Swapping username and password variables.
Forgetting the colon separator.
5fill in blank
hard

Fill all three blanks to build a Basic authentication header from variables and encode it properly.

Rest API
import base64
username = 'alice'
password = 'wonderland'
credentials = f"[1]:[2]"
encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode([3])
headers = {'Authorization': 'Basic ' + encoded_credentials}
Drag options to blanks, or click blank then click option'
Ausername
Bpassword
C'utf-8'
D'ascii'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names.
Decoding with 'ascii' which may cause errors.
Not joining username and password with a colon.