Challenge - 5 Problems
HttpResponse Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Django view return?
Consider this Django view function. What is the output when a client requests this view?
Django
from django.http import HttpResponse def my_view(request): response = HttpResponse('Hello World', content_type='text/plain') response.status_code = 201 return response
Attempts:
2 left
💡 Hint
Look at the content_type and status_code set on the HttpResponse object.
✗ Incorrect
The HttpResponse is created with 'Hello World' as content and content_type 'text/plain'. The status_code is explicitly set to 201, so the response will have that status code.
📝 Syntax
intermediate2:00remaining
Which option correctly creates an HttpResponse with a custom header?
You want to return an HttpResponse with the header 'X-Custom: 123'. Which code snippet does this correctly?
Attempts:
2 left
💡 Hint
HttpResponse headers are set like dictionary keys on the response object.
✗ Incorrect
Option D correctly sets the header by assigning to response['X-Custom']. Option D is invalid because HttpResponse does not accept a headers argument. Option D is invalid because response.headers attribute does not exist. Option D is invalid because set_header method does not exist.
❓ state_output
advanced2:00remaining
What is the content of this HttpResponse after modification?
Given this code, what will be the final content of the HttpResponse object?
Django
response = HttpResponse('Start') response.content += b' Middle' response.content = response.content.replace(b'Start', b'Begin') response.content += b' End'
Attempts:
2 left
💡 Hint
HttpResponse.content is bytes, so string operations must use bytes.
✗ Incorrect
Initially content is b'Start'. Adding b' Middle' makes it b'Start Middle'. Replacing b'Start' with b'Begin' changes it to b'Begin Middle'. Adding b' End' results in b'Begin Middle End'.
🔧 Debug
advanced2:00remaining
Why does this HttpResponse raise an error?
This code raises an error. What is the cause?
Django
response = HttpResponse() response.content = 'Hello World' print(response.content)
Attempts:
2 left
💡 Hint
HttpResponse.content expects bytes, not a string.
✗ Incorrect
HttpResponse.content must be bytes. Assigning a string causes a TypeError when Django tries to process it.
🧠 Conceptual
expert2:00remaining
What happens if you set HttpResponse.status_code to 404 after returning it?
If you create an HttpResponse and return it from a view, then later in middleware you set response.status_code = 404, what will the client receive?
Attempts:
2 left
💡 Hint
HttpResponse objects are mutable until sent to the client.
✗ Incorrect
HttpResponse objects can be modified after creation and before sending. Changing status_code in middleware affects the final response sent to the client.