How to set response status code

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to set response status code

Pete H

Hi,

I'd like my page handlers to be able to return a dynamically created
page in the ususal way, but sometimes set an HTTP response code of
something other than the usual 200.
Can I just do cherrypy.response.status = code before the handler's
return statement, or must I do something more complicated, and if so,
what?

Thanks,
Pete H


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to set response status code

James Mills-3

On Sat, Oct 27, 2007 at 07:08:52AM -0700, Pete H wrote:
> I'd like my page handlers to be able to return a dynamically created
> page in the ususal way, but sometimes set an HTTP response code of
> something other than the usual 200.
> Can I just do cherrypy.response.status = code before the handler's
> return statement, or must I do something more complicated, and if so,
> what?

I believe you do something like this:

class Root:

   def foo(self):
      cherrypy.reponse.headers["Status"] = "404"
      return "Not Found"

As an example of course :)

cheers
James

--
--
-"Problems are Solved by Method"
-
- James Mills <[hidden email]>
- HomePage: http://shortcircuit.net.au/~prologic/
- IRC: irc://shortcircuit.net.au#se

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to set response status code

Robert Brewer-4
In reply to this post by Pete H

Pete H wrote:
> I'd like my page handlers to be able to return a dynamically created
> page in the ususal way, but sometimes set an HTTP response code of
> something other than the usual 200.
> Can I just do cherrypy.response.status = code before the handler's
> return statement, or must I do something more complicated, and if so,
> what?

Exactly right. You can set it to either an int or a code + reason
phrase:

    cherrypy.response.status = 201

or

    cherrypy.response.status = "201 Resource Created"


Robert Brewer
[hidden email]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to set response status code

Pete H



On Oct 27, 5:40 pm, "Robert Brewer" <[hidden email]> wrote:

> Pete H wrote:
> > I'd like my page handlers to be able to return a dynamically created
> > page in the ususal way, but sometimes set an HTTP response code of
> > something other than the usual 200.
> > Can I just do cherrypy.response.status = code before the handler's
> > return statement, or must I do something more complicated, and if so,
> > what?
>
> Exactly right. You can set it to either an int or a code + reason
> phrase:
>
>     cherrypy.response.status = 201
>
> or
>
>     cherrypy.response.status = "201 Resource Created"
>
> Robert Brewer
> [hidden email]

Thanks Robert. So essentially cherrypy here is the thread instance
that's handling my request?

 I'm still trying to work out a fix for my 405 when doing an
unauthenticated post  - this is my first time at getting into the guts
of an OO system and it's taking a while to get my head round it. But I
really like CherrtyPy so far. I doesn't try to make me fit some -one
elses idea of how my app should work.

Pete H


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to set response status code

Robert Brewer-4

Pete H wrote:

> On Oct 27, 5:40 pm, "Robert Brewer" <[hidden email]> wrote:
> > Pete H wrote:
> > > I'd like my page handlers to be able to return a dynamically
> created
> > > page in the ususal way, but sometimes set an HTTP response code of
> > > something other than the usual 200.
> > > Can I just do cherrypy.response.status = code before the handler's
> > > return statement, or must I do something more complicated, and if
> so,
> > > what?
> >
> > Exactly right. You can set it to either an int or a code + reason
> > phrase:
> >
> >     cherrypy.response.status = 201
> >
> > or
> >
> >     cherrypy.response.status = "201 Resource Created"
>
> Thanks Robert. So essentially cherrypy here is the thread instance
> that's handling my request?

Yes, although there's a cherrypy.serving object that does the actual
threadlocal handling; it obtains new Request and Response objects for
each request, and cherrypy.request and .response are proxied to
cherrypy.serving.request and .response, respectively. This allows:

  cherrypy to be a globally shared module that all apps can import
  .serving to be a threadlocal, and
  .request/response to be created and destroyed easily per request

> I'm still trying to work out a fix for my 405 when doing an
> unauthenticated post  - this is my first time at getting into the guts
> of an OO system and it's taking a while to get my head round it. But I
> really like CherrtyPy so far. I doesn't try to make me fit some -one
> elses idea of how my app should work.

Yeah, I'm working on that right now. There are a couple of different
ways that the server might not read the entire request and yet not close
the connection. It should really do one or the other always. It's just
going to take a little while to write good tests for that. ;) Once
that's working, I should be able to close a number of open tickets,
yours included.


Robert Brewer
[hidden email]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to set response status code

Pete H



On Oct 27, 6:46 pm, "Robert Brewer" <[hidden email]> wrote:

> Pete H wrote:
> > On Oct 27, 5:40 pm, "Robert Brewer" <[hidden email]> wrote:
> > > Pete H wrote:
> > > > I'd like my page handlers to be able to return a dynamically
> > created
> > > > page in the ususal way, but sometimes set an HTTP response code of
> > > > something other than the usual 200.
> > > > Can I just do cherrypy.response.status = code before the handler's
> > > > return statement, or must I do something more complicated, and if
> > so,
> > > > what?
>
> > > Exactly right. You can set it to either an int or a code + reason
> > > phrase:
>
> > >     cherrypy.response.status = 201
>
> > > or
>
> > >     cherrypy.response.status = "201 Resource Created"
>
> > Thanks Robert. So essentially cherrypy here is the thread instance
> > that's handling my request?
>
> Yes, although there's a cherrypy.serving object that does the actual
> threadlocal handling; it obtains new Request and Response objects for
> each request, and cherrypy.request and .response are proxied to
> cherrypy.serving.request and .response, respectively. This allows:
>
>   cherrypy to be a globally shared module that all apps can import
>   .serving to be a threadlocal, and
>   .request/response to be created and destroyed easily per request
>
> > I'm still trying to work out a fix for my 405 when doing an
> > unauthenticated post  - this is my first time at getting into the guts
> > of an OO system and it's taking a while to get my head round it. But I
> > really like CherrtyPy so far. I doesn't try to make me fit some -one
> > elses idea of how my app should work.
>
> Yeah, I'm working on that right now. There are a couple of different
> ways that the server might not read the entire request and yet not close
> the connection. It should really do one or the other always. It's just
> going to take a little while to write good tests for that. ;) Once
> that's working, I should be able to close a number of open tickets,
> yours included.
>
> Robert Brewer
> [hidden email]

Robert,

I'm glad to hear it's not trivial ;-)

Pete H


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: How to set response status code

Musiitwa Edmond
In reply to this post by Robert Brewer-4

Thanks a lot i used 

Response.status =  "status code"
and it worked perfectly for me.
thanks alot.

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.