PCPP 32 10x PCPP Certified Professional Python Programmer 1 · Free Practice Question Medium
Question 5
Consider the following code:
- response = requests.get('https://api.github.com')
The response of a request often has some valuable information, known as a payload, in the request body. Which of the following methods and properties are defined correctly to view the content of a response? (Select two)
-
A
response.contentreturns the response content as astringobject. -
B
response.textreturns the response content as abytesobject. -
C
response.json()returns the response content as adictobject. -
D
response.textreturns the response content as astringobject.
Reveal correct answers
Correct answers: C, D
A.
The content attribute of a response object in the requests library returns the response content as a bytes object, not as a string object. This is useful when the response content is expected to be in binary format, such as images, PDFs, or other non-text data.
B.
The text attribute of a response object in the requests library returns the response content as a string object, not as a bytes object. Bytes objects represent binary data, while response.text is used specifically to access the response content as text.
C.
The json() method of a response object in the requests library is used to decode the response content assuming it is in JSON format. It returns the response content as a dictionary object (dict) in Python. This is helpful when the response content is in JSON format and needs to be easily accessible and manipulable as a structured data object in Python.
D.
The text attribute of a response object in the requests library returns the response content as a string object. This is useful when the response content is expected to be in text format, such as HTML, plain text, or JSON encoded as a string.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
