Bottle 应用 mongo db
Bottle app with mongo db
我在 Bottle 和 Mongo 数据库的帮助下尝试通用 REST API。
我在地址 127.0.0.1:8010/ 的网页上遇到的错误是
Error: 404 Not Found
Sorry, the requested URL http ://127.0.0.1:8010/ caused an error:
Not found: '/'
在命令行中,我得到这个:
$ python myrestapi.py
Bottle v0.12.10 server starting up (using WSGIRefServer())...
Listening on "http://127.0.0.1:8010/"
Hit Ctrl-C to quit.
127.0.0.1 - - [17/Dec/2016 01:54:46] GET / HTTP/1.1 404 720
这是我的 documents/myrestapi.py 代码:
import json
import bottle
from bottle import route, run, request, abort
from pymongo import Connection
connection = Connection('localhost', 27017)
db = connection.mydatabase
app = bottle.Bottle()
@app.route('/documents', method='PUT')
def put_document():
data = request.body.readline()
if not data:
abort(400, 'No data received')
entity = json.loads(data)
if not entity.has_key('_id'):
abort(400, 'No _id specified')
try:
db['documents'].save(entity)
except ValidationError as ve:
abort(400, str(ve))
@app.route('/documents/:id', method='GET')
def get_document(id):
entity = db['documents'].find_one({'_id':id})
if not entity:
abort(404, 'No document with id %s' % id)
return entity
bottle.run(host='localhost', port=8010)
Error: 404 Not Found
当我们查找的 restful api 不可用时,会抛出 404 错误。
在这种情况下,如果您尝试了 /documents
或 /documents/1
,那么您将收到回复,因为您有 @app.route('/documents', method='PUT')
和 @app.route('/documents/:id', method='GET')
更多信息请参考
http://www.restapitutorial.com/lessons/httpmethods.html
REST API 404: Bad URI, or Missing Resource?
我在 Bottle 和 Mongo 数据库的帮助下尝试通用 REST API。
我在地址 127.0.0.1:8010/ 的网页上遇到的错误是
Error: 404 Not Found
Sorry, the requested URL http ://127.0.0.1:8010/ caused an error:
Not found: '/'
在命令行中,我得到这个:
$ python myrestapi.py
Bottle v0.12.10 server starting up (using WSGIRefServer())...
Listening on "http://127.0.0.1:8010/"
Hit Ctrl-C to quit.
127.0.0.1 - - [17/Dec/2016 01:54:46] GET / HTTP/1.1 404 720
这是我的 documents/myrestapi.py 代码:
import json
import bottle
from bottle import route, run, request, abort
from pymongo import Connection
connection = Connection('localhost', 27017)
db = connection.mydatabase
app = bottle.Bottle()
@app.route('/documents', method='PUT')
def put_document():
data = request.body.readline()
if not data:
abort(400, 'No data received')
entity = json.loads(data)
if not entity.has_key('_id'):
abort(400, 'No _id specified')
try:
db['documents'].save(entity)
except ValidationError as ve:
abort(400, str(ve))
@app.route('/documents/:id', method='GET')
def get_document(id):
entity = db['documents'].find_one({'_id':id})
if not entity:
abort(404, 'No document with id %s' % id)
return entity
bottle.run(host='localhost', port=8010)
当我们查找的 restful api 不可用时,会抛出Error: 404 Not Found
404 错误。
在这种情况下,如果您尝试了 /documents
或 /documents/1
,那么您将收到回复,因为您有 @app.route('/documents', method='PUT')
和 @app.route('/documents/:id', method='GET')
更多信息请参考
http://www.restapitutorial.com/lessons/httpmethods.html
REST API 404: Bad URI, or Missing Resource?