如何在 Linux 终端中使用 curl POST 功能在 python 中创建文档

how to use the curl POST function in Linux terminal to create a document in python

我正在尝试将新记录添加到 python 文档中,我认为我遇到了由 curl post 函数引起的问题。我已将 python 文件和 posting 时收到的错误附加到我的 url。谁能指出我正确的方向。

我不明白错误代码以确定问题是否来自 python 代码,但我确实怀疑 curl url.

有问题
#!/usr/bin/python
import json
from bson import json_util
from bson.json_util import dumps
import bottle
from bottle import route, run, request, abort
#imports for database
from pymongo import MongoClient
connection = MongoClient('localhost', 27017)
db = connection['city']
collection = db['inspections']

# set up URI paths for REST service
@route('/hello', method='GET')
def get_hello():
    word = '"'+request.GET.get('name', None)+'"'
    string="{hello:"+word+"}"
    return json.loads(json.dumps(string, indent=4, default=json_util.default))

@route('/strings', method='POST')
def run_post():
    first = '"'+request.json.get('string1')+'"'
    second = '"'+request.json.get('string2')+'"'
    data="{first:"+first+",second:"+ second+"}"
    return json.loads(json.dumps(data, indent=4, default=json_util.default))


@route('/create', method='POST')
def run_create(): 
    myid = request.json.get('id')
    print(myid)
    cert_number = request.json.get('certificate_number')
    bus_name = request.json.get('business_name')
    date = request.json.get('date')
    result = request.json.get('result')
    sector = request.json.get('sector')
    added_id = collection.insert({"id":myid,"certificate_number":cert_number,"business_name":bus_name,"date":date,"result":result,"sector":sector})
    added_doc = collection.find_one({"_id":added_id})
    return json.loads(json.dumps(added_doc, indent=4, default=json_util.default))

#url does not allow spacing when passing an argument,
#therefore i use underscores when passing the business_name and the remove them
#when creating the query
@route('/read', method='GET')
def get_read():
    word = request.params.get('business_name')
    word = word.replace("_"," ")
    found_doc = collection.find({"business_name":{'$regex':word}}) #will still get results when user pass parameter with white space
    return dumps(found_doc)

@route('/update', method='GET')
def get_update(rslt = "Violation Issued"):
    myid = request.query.id
    query = { "id" :myid}
    new_update =  { "$set":{"result":rslt}}
    collection.update_one(query,new_update)
    updated_doc = collection.find_one({"id":myid})
    return json.loads(json.dumps(updated_doc, indent=4, default=json_util.default))

@route('/delete', method='GET')
def get_update():
    myid = request.query.id
    query = {"id" :myid};
    print(query)
    result = collection.delete_one(query)
    return "document with id "+myid+" Has beed deleted from the City Collection"



if __name__ == '__main__':
    run(debug=True,reloader = True)
    #run(host='localhost', port=8080)

错误:

返回HTML:

python 错误:

问题是,在您的 curl 请求中的 json 中的某个时刻,您使用了 而不是 "。因此 json 解析器抛出一个错误。

所以

"business_name" : “ACME Test INC."

写:

"business_name" : "ACME Test INC."

不确定你是否解决了这个问题,但我们开始吧。 Jakob 是正确的,您使用 而不是 " 接下来,从您要插入的文档中获取值

data = request.json (Contains parsed content)

给你需要的变量赋值比如id

id = data['id']

将所有值存储在一个字典变量中(我认为这样更干净)

document = {"id":myid,"certificate_number":cert_number,"business_name":bus_name,"date":date,"result":result,"sector":sector}

最后,使用 insert_one 并捕获错误

try:
    collection.insert_one(document)
    print("CREATED NEW DOCUMENT")

except Exception as e:
    print("ERROR: ", e)

另外,有几种方法可以解决您提到的 cURL 请求的 "space" 问题。 一种方法是简单地在空格之间添加一个 %20,如下所示:

ACME%20TEST%20INC.

希望对您有所帮助。