2013-05-19 14 views
17

ボトルで自分のレスポンスのHTTPステータスコードを設定するにはどうすればよいですか?ボトルにHTTPステータスコードを設定していますか?

from bottle import app, run, route, Response 

@route('/') 
def f(): 
    Response.status = 300 # also tried `Response.status_code = 300` 
    return dict(hello='world') 

'''StripPathMiddleware defined: 
    http://bottlepy.org/docs/dev/recipes.html#ignore-trailing-slashes 
''' 

run(host='localhost', app=StripPathMiddleware(app())) 

あなたが見ることができるように、出力は私が設定したHTTPステータスコードは戻りません:

$ curl localhost:8080 -i 
HTTP/1.0 200 OK 
Date: Sun, 19 May 2013 18:28:12 GMT 
Server: WSGIServer/0.1 Python/2.7.4 
Content-Length: 18 
Content-Type: application/json 

{"hello": "world"} 
+0

'from bottle import response; response.status = 300 'は動作しますか? http://bottlepy.org/docs/dev/api.html#bottle.response – dm03514

+1

はい、それはやりました。ありがとう:) –

答えて

30

を、私はあなたがresponse

from bottle import response; response.status = 300

12

構築されたボトルのを使用する必要があります信じています応答形式ではステータスコードを正常に処理します。以下のようなものを考えてみましょう:

return bottle.HTTPResponse(status=300, body=theBody) 

のように:

import json 
from bottle import HTTPResponse 

@route('/') 
def f(): 
    theBody = json.dumps({'hello': 'world'}) # you seem to want a JSON response 
    return bottle.HTTPResponse(status=300, body=theBody) 
+0

dm03514の答えは私が探していたものでした。私のコードに変更を加えることなく、 'Response'から' response'に名前を変更することを除いて、私が全てのことを私に与えました。 –

-1

昇給はステータスコード(200302401)を表示するためにHTTPResponseはでより多くの電力を得るために使用することができます:

あなたは、単にこれを行うことができますのようにway:

import json 
from bottle import HTTPResponse 

response={} 
headers = {'Content-type': 'application/json'} 
response['status'] ="Success" 
response['message']="Hello World." 
result = json.dumps(response,headers) 
raise HTTPResponse(result,status=200,headers=headers) 
関連する問題