Bottle app with mongo db

716 Views Asked by At

I am trying a general REST API with the help of Bottle and Mongo Database.

The error that I am getting on the web page on address 127.0.0.1:8010/ is

Error: 404 Not Found

Sorry, the requested URL http ://127.0.0.1:8010/ caused an error:

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 :

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)
1

There are 1 best solutions below

1
On BEST ANSWER

Error: 404 Not Found

404 error is thrown when an restful api which we look for is not available.

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')

For more information please refer

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

REST API 404: Bad URI, or Missing Resource?