简体   繁体   English

带有Mongo DB的Bottle应用

[英]Bottle app with mongo db

I am trying a general REST API with the help of Bottle and Mongo Database. 我正在借助Bottle和Mongo数据库尝试通用的REST API。

The error that I am getting on the web page on address 127.0.0.1:8010/ is 我在地址127.0.0.1:8010/的网页上遇到的错误是

Error: 404 Not Found 未找到错误404

Sorry, the requested URL http ://127.0.0.1:8010/ caused an error: 抱歉,请求的URL http://127.0.0.1:8010 /导致错误:

Not found: '/' 未找到: '/'

In command line, I am getting this: 在命令行中,我得到这个:

$ 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

Here is my code for documents/myrestapi.py : 这是我的document / 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 未找到错误404

404 error is thrown when an restful api which we look for is not available. 当我们寻找的静态API不可用时,抛出404错误。

In this case if you have tried /documents or /documents/1 then you will get a response, since you have @app.route('/documents', method='PUT') and @app.route('/documents/:id', method='GET') 在这种情况下,如果您尝试了/documents/documents/1 ,则将得到响应,因为您拥有@app.route('/documents', method='PUT')@app.route('/documents/:id', method='GET')

For more information please refer 有关更多信息,请参阅

http://www.restapitutorial.com/lessons/httpmethods.html http://www.restapitutorial.com/lessons/httpmethods.html

REST API 404: Bad URI, or Missing Resource? REST API 404:URI错误或资源丢失?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM