-3

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

5
  • Why would you need to do this?
    – Reez0
    Sep 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 Bot
    Sep 21, 2022 at 10:30
  • What does this have to do with #django-rest-framework?
    – Andrew
    Sep 21, 2022 at 10:34
  • @Reez0 To fix bugs in already listed apps
    – stick Cour
    Sep 21, 2022 at 11:45
  • @Andrew I'm using DRF in this project....
    – stick Cour
    Sep 21, 2022 at 11:47

2 Answers 2

0

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

5
  • If I use HttpResponse "CUSTOM MESSAGE" will in the body, I want it in the http header
    – stick Cour
    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
    – sebsch
    Sep 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"}
    – stick Cour
    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)
    – nigel239
    Sep 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,
    – stick Cour
    Sep 21, 2022 at 15:28
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.