So, I know the sever code works, as this site is currently in production and working fine. I, on the other hand, have developed a test script to unit test the API. I'm trying, using the HTTP POST method, to send a PDF file over to the nginx -> gunicorn -> flask app environment of the server.
The server gets the attachments with:
@app.route('<ObjectId:_id>/attachments', methods=['POST'])
@csrf.exempt
@opportunity_owner_required
@auth.login_required
@nocache
def upload_attachment(_id):
upload = request.files.get('file')
if not upload:
abort(400, "Missing attached file.")
and I try and pass the pdf file to the server with:
def test_add_opportunity_attachment(self):
files = {'file': ("mozilla.pdf", open('%s/mozilla.pdf' % PATHTOFILE, 'rb'))}
headers = {'Content-Type': 'application/pdf', "enctype": "multipart/form-data", "Content-Disposition":"attachment;filename=mozilla.pdf"}
r=self.session.post(self.formatURL('/attachments'), headers=headers, files=files)
assert r.status_code == 200
But I always get the status 400.
When using ngrep to follow the output, I do see what appears to be the encoded form of the PDF being passed across the network, but the server cannot see it.
Please note: Some of the information is missing, due to the proprietary nature of it. But all the functions used within the test function work fine. formatURL formats it as expected and the urls do match.
formatURLis returning a valid url I hope. I don't have time to look up flask's documentation now, but someone else might know better about the files attribute. Since in amultipart/form-datarequest there could be multiple files, are you sure.get('file')is correct?formatURLreturns the right url, yes. The object is being sent in as{'file': (filename, fileref)}so.get('file')/should/ be pulling out the tuple with thefilenameandfileref.test_add_opportunity_attachmentmethod that deals with logging in, butupload_attachmenthas@auth.login_required. Is the login information part of the missing information you mentioned?