2

It seems Webapp2 does not support the status code 418 because it doesn't have a status message mapped to it. How do I get around it and return a 418?

response.set_status(418)    # does not work
response.status_int = 418   # does not work either

Traceback:

  File "/.../App Engine SDK/lib/webapp2-2.5.2/webapp2.py", line 425, in set_status
    self.status = code
  File "/.../App Engine SDK/lib/webapp2-2.5.2/webapp2.py", line 405, in _set_status
    message = message or Response.http_status_message(code)
  File "/.../App Engine SDK/lib/webapp2-2.5.2/webapp2.py", line 488, in http_status_message
    raise KeyError('Invalid HTTP status code: %d' % code)
KeyError: 'Invalid HTTP status code: 418'
10
  • I'm intrigued as to what the use case is....
    – Kai
    Commented Aug 11, 2017 at 20:20
  • save418.com Commented Aug 11, 2017 at 20:20
  • @Kai The server should return 418 when someone is trying to brew coffee with a teapot ;) Commented Aug 11, 2017 at 20:22
  • Doesn't it work? What happens? Looking at the source code it seems like it uses WebOb, which does include 418: github.com/Pylons/webob/blob/master/src/webob/util.py#L103
    – jonrsharpe
    Commented Aug 11, 2017 at 20:27
  • @jonrsharpe Added traceback. Edit: Looking inside statusreasons.py in the App Engine SDK, 418 is indeed missing. Commented Aug 11, 2017 at 20:29

1 Answer 1

2

By monkey patching, I am able to get the status code 418 to work, but only partially.

class TeapotHandle(webapp2.RequestHandler):
    def get(self):
        from webob import util
        import httplib

        util.status_reasons[418] = "I'm a teapot"
        httplib.responses[418] = "I'm a teapot"

        self.response.set_status(418)

yields, using the dev server:

HTTP/1.1 418 Unknown Status Code
[...]

But in fact, it works as expected once deployed on App Engine Cloud:

HTTP/1.1 418 I'm a Teapot
[...]

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