Opened 6 years ago

Closed 6 years ago

#18678 closed Bug (fixed)

HTTPResponse subclasses don't accept base class parameters

Reported by: hp1337@… Owned by: nobody
Component: HTTP handling Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I would like to pass content_type when creating a new HttpResponseNotAllowed object. Unfortunately, it does not accept parameters from its base class, HttpResponse. Doing the following results in an error:

return HttpResponseNotAllowed(content_type="application/json", permitted_methods=['POST'], )

There is a simple fix to this. See my patch below. This patch should be applied to other HttpResponse sub-classes as well. Namely, HttpResponseRedirect and HttpResponsePermanentRedirect.

class HttpResponseNotAllowed(HttpResponse):
    status_code = 405

    def __init__(self, permitted_methods, *args, **kwargs):
        super(HttpResponseNotAllowed, self).__init__(*args, **kwargs)
        self['Allow'] = ', '.join(permitted_methods)

Attachments (1)

18678-1.diff (5.0 KB) - added by Claude Paroz 6 years ago.
Allow HttpResponse arguments for subclasses

Download all attachments as: .zip

Change History (6)

comment:1 Changed 6 years ago by Preston Holmes

Can you provide more detail about your use case - while I can't find anything in a quick review of the spec that prohibits setting a content-type for a 405 response, it is hard to imagine a client that would do something specific with it.

If this is in fact something that should be supported - it should be supported for most (all?) included subclasses of HttpResponse.

comment:2 Changed 6 years ago by Claude Paroz

Triage Stage: UnreviewedAccepted
Version: 1.3master

I also think that we should allow HttpResponse standard parameters for all subclasses. I don't see anything in the HTTP spec that prevents a response to have attached content (with content-type).

Changed 6 years ago by Claude Paroz

Attachment: 18678-1.diff added

Allow HttpResponse arguments for subclasses

comment:3 Changed 6 years ago by Claude Paroz

Has patch: set

comment:4 Changed 6 years ago by anonymous

Sorry, I'm a bit late. My use case is a proprietary RESTful client that will reject all communication that is not of a specific content-type. Even if the status code is correct.

comment:5 Changed 6 years ago by Claude Paroz <claude@…>

Resolution: fixed
Status: newclosed

In [44c09de555f83b0596a0886e3f51a9a84368d036]:

Fixed #18678 -- HttpResponse init arguments allowed for subclasses

Thanks hp1337@… for the report.

Note: See TracTickets for help on using tickets.
Back to Top