I am trying to create a REST API application using python bottle framework
I'd like to be able to insert data in mongodb via HTTP PUT request.
So far I am able to get response from the mongodb using HTTP GET.
Please help me INSERT data in mongodb via HTTP PUT.
JSON format I have to insert as follows:
{"_id": "id_1", "key_1": "value_1"}
[i am using this extension to get and put http response]
import json
import bottle
from bottle import route, run, request, abort
from pymongo import Connection
connection = Connection('localhost', 27017)
db = connection.mydatabase
@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))
@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
run(host='localhost', port=8080)