Code

#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

Description

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 claudep 20 months ago.
Allow HttpResponse arguments for subclasses

Download all attachments as: .zip

Change History (6)

comment:1 Changed 21 months ago by ptone

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 20 months ago by claudep

  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.3 to master

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 20 months ago by claudep

Allow HttpResponse arguments for subclasses

comment:3 Changed 20 months ago by claudep

  • Has patch set

comment:4 Changed 20 months 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 20 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

In [44c09de555f83b0596a0886e3f51a9a84368d036]:

Fixed #18678 -- HttpResponse init arguments allowed for subclasses

Thanks hp1337@… for the report.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.