Nips
Nips

Reputation: 13870

How to send custom status code?

I have tornado handler:

class DumbHandler(tornado.web.RequestHandler):

    def get(self, dumb):
        self.clear()
        self.set_status(555)
        self.write(
            json.dumps({
                'error_code': 211
            })
        )

but I have exception:

ValueError: ('unknown status code %d', 555)

I want to send my own status code (555), how to do it?

Upvotes: 2

Views: 1462

Answers (2)

dcai
dcai

Reputation: 2555

555 is not valid status code, you must use the status code listed here:

https://docs.python.org/3/library/http.html#http-status-codes

Or, you can add reason when use set_status(555, "my custom status code").

Upvotes: 0

gre_gor
gre_gor

Reputation: 6807

according to the documentation for RequestHandler.set_status(status_code, reason=None):

  • status_code (int) – Response status code. If reason is None, it must be present in httplib.responses.

you need to provide a reason for custom status codes.

You can put an empty string for a reason:

self.set_status(555, "")

Upvotes: 2

Related Questions