我在Bottle和Mongo数据库的帮助下尝试使用一般的REST API。mongo db的瓶子应用程序
,我得到网页上的地址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
这里是我的代码对于文件/ 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)
我修正了垂直间距,但请修正Python代码的缩进 –
另外,你有n o在'/'设置的路由,所以你尝试过'/ documents/1',或者任何ID? –