I'm trying to be compatible with an app-side bug(). I want to return a response with custom HTTP message
For example, I get HTTP/1.1 429 TOO_MANY_REQUESTS
now, I want get HTTP/1.1 429 CUSTOM_MESSAGE
Howerver, I can get only h2 200
-
Why would you need to do this?– Reez0Sep 21, 2022 at 10:08
-
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.– Community BotSep 21, 2022 at 10:30
-
What does this have to do with #django-rest-framework?– AndrewSep 21, 2022 at 10:34
-
@Reez0 To fix bugs in already listed apps– stick CourSep 21, 2022 at 11:45
-
@Andrew I'm using DRF in this project....– stick CourSep 21, 2022 at 11:47
2 Answers
Django has HttpResponse objects to build custom responses.
The following should do the trick:
from django.http import HttpResponse
response = HttpResponse("CUSTOM MESSAGE", status=429, headers={"CUSTOM_MESSAGE": "..."})
see: https://docs.djangoproject.com/en/4.1/ref/request-response/#id4
-
If I use HttpResponse "CUSTOM MESSAGE" will in the body, I want it in the http header Sep 21, 2022 at 10:05
-
than just add this to the headers field HttpResponse(status=429, headers={"CUSTOM_MESSAGE": "this is really too much to handle for my server. Please go away!"}) Edit: Updated the answer– sebschSep 21, 2022 at 10:08
-
Sorry for the ambiguity what I said, I want to edit the content in http message header which looks like "OK" in "http1.1 200 OK", not the header like {"content-type": "text/html"} Sep 21, 2022 at 11:54
-
It's called a
HTTP status code
. Google that, plenty of answers will be shown. You likely do not want to invent your own. Here is a list of statuscodes and their meanings: umbraco.com/knowledge-base/http-status-codes (scroll down)– nigel239Sep 21, 2022 at 12:52 -
Actually, my goal is editing the status text,textual description of the status code. Made it substandard for my particular needs, Sep 21, 2022 at 15:28
Updating HttpResponse.reason_phrase work for me, but HTTP/2.0 message will not show reason phrase, I could only find it in HTTP/1.1 message
see: https://docs.djangoproject.com/en/4.1/ref/request-response/#django.http.HttpResponse.reason_phrase