Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send dict as response in Python Bottle with custom status code

import bottle
from bottle import route, run

@route('/', method='GET')
def homepage():
    return {'foo' : 'bar'}

if __name__=='__main__':
    bottle.debug(True)
    run(host='0.0.0.0', port= 8080, reloader = True)

This config will return a json object representing the dict from homepage with HTTP status code 200. What should I do to return the same content but with, say, 202 status code?

like image 914
Thiago Moraes Avatar asked Dec 15 '22 22:12

Thiago Moraes


1 Answers

You can set the response.status attribute:

from bottle import response

@route('/', method='GET')
def homepage():
    response.status = 202
    return {'foo' : 'bar'}
like image 133
Ned Batchelder Avatar answered Apr 30 '23 04:04

Ned Batchelder