4

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)
2
  • 2
    Could you please fix the formatting and tell us what you have tried, what you would expect, what did not work, how does the error message look like, ... Commented Oct 8, 2012 at 19:27
  • I want to insert the data in the following format {"_id": "doc1", "name": "Test Document 1"} Commented Oct 9, 2012 at 3:14

1 Answer 1

2

The following revised version of your code seems to work as you intended:

import json

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()
    print(data)
    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.insert(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)

I have MongoDB running and executing the script, start the server on localhost port 8080, I then execute the following commands on a shell with the expected results:

$ echo '{"_id": "id_1", "key_1": "value_1", "key_2": "value_2"}' | curl -X PUT -d @- http://localhost:8080/documents
$ curl http://localhost:8080/documents/id_1
{"_id": "id_1", "key_1": "value_1", "key_2": "value_2"}
Sign up to request clarification or add additional context in comments.

4 Comments

what you are doing is write , but i have to send http put request to the my localhost server so that it will insert the data in momgodb
I am sorry but I don't understand your comment. The answer I have provided, based on your code, is doing exactly that: running on localhost and inserting and retrieving data from a MongoDB instance running on localhost. Can you please explain more clearly why it doesn't solve your problem?
The same thing you are doing by executing curl in the terminal. i have to do it by using put method from browser
The curl command lines were just to demonstrate that the server works. It will work just as well from the browser. You can test it with the Chrome extension you've mentioned in your question and it will work exactly the same. The purpose of the question the server side, which is solved, not the client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.