0

Hello I am having trouble updating a MongoDB document while using a Python REST API. Here is the Python code:

import json
import bottle
from bson import json_util
from bson.json_util import dumps
from pymongo import MongoClient
from bottle import route, run, request, abort

connection = MongoClient('localhost', 27017)
db = connection['city']
collection = db['inspections']

#Create 
@route('/create', method='POST')
def post_create():

    data=json.load(request.body)
    id=data["id"]
    certificate_number=data["certificate_number"]   
    business_name=data["business_name"]
    date=data["date"]
    result=data["result"]
    sector=data["sector"]
    result=collection.insert_one({'id': id, 'certificate_number': certificate_number, 'business_name' : business_name, 'date' : date, 'result' : result, 'sector' : sector}).inserted_id
    return json.loads(json.dumps(result, indent=4, default=json_util.default))

#Update
@route('/update', method='GET')
def update():

    id=request.query.id
    result=request.query.result
    myquery = {"id": "10011-2017-TEST" }
    newvalues = { "$set": { "result" : result } }
    collection.update(myquery, newvalues)

After I start the above Python code, these are the two queries I am running in my terminal:

#Create
curl -H "Content-Type: application/json" -X POST -d '{"id" : "10011-2017-TEST","certificate_number" : 9278833,"business_name" : "ACME TEST INC.","date" : "Feb 20 2017","result" : "No Violation Issued","sector" : "Test Retail Dealer - 101"}' http://localhost:8080/create

#Update
curl http://localhost:8080/update?id="10011-2017-TEST"&result="Violation Issued"

The first curl command works perfectly. However, when I run the second curl update command, I receive an empty "result".

{
        "_id" : ObjectId("5e48b39618882a084aa35b86"),
        "sector" : "Test Retail Dealer - 101",
        "certificate_number" : 9278833,
        "result" : "",
        "date" : "Feb 20 2017",
        "business_name" : "ACME TEST INC.",
        "id" : "10011-2017-TEST"
}

I need the "result" to change from "No Violation Issued" to "Violation Issued". I think I am close, because I know the second update curl command is referencing the correct document, however, it is not updating the "result" properly. I am suspecting the python function can not see the second part of the query, resulting in a blank update.

1

1 Answer 1

1

axme100 is correct, the whitespace is your problem. There's few ways to work around it different shell quotes /escapes etc. but the easiest is probably:

curl 'http://localhost:8080/update?id=10011-2017-TEST&result=Violation%20Issued'

Put the whole thing in single quotes whenever you have an & in the query string to prevent the shell running it in the background.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.