I was learning flask or trying to along with mongodb and things were mostly going okay. Though I am having a heck of a time figuring out what object is coming back to my little test_api.py of my api.py flask endpoint.
api.py
#uses mongo engine
#simple data structure
class SimpleDocument(db.Document):
docName = db.StringField()
docAction = db.StringField()
def to_json(self):
return {"id": self.id,
"name":self.docName,
"action":self.docAction}
@app.route('/document/', methods=["POST"])
def add_simpledocument():
body = request.get_json()
simpledoc= SimpleDoc(**body).save()
print(simpledoc.to_json())
return simpledoc.to_json(), 201 # i would like to return the object so I have the ID of the object just created too...
now in my test_api.py I have:
def test_addSimpleDoc():
url = 'http://localhost:5000/document/'
headers = {'Content-Type': 'application/json'}
payload = {'name':"Simple Name Test", 'action':'What the doc does'}
resp=requests.post(url, headers=headers, data=json.dumps(payload,indent=4))
print(resp.json["id"])
#cannot figure out how to get the id of the object I just created...
Fials with a print(resp.json["id"]) method is not subscriptable. Clearly I have no idea what type of object is coming back to me.