-1

I try to upload an Image to a python Flask API. The Flask code is based on following snippets and tutorials:

I have already tried different solutions presented here on stackoverflow on this topic but they all couldn't solve the issues.

My Flask code

@app.route("/rupload/<filename>", methods=['POST', 'PUT'])
def rupload(filename):
 # Sanity checks and setup skipped.
 filename = secure_filename(filename)
 fileFullPath = os.path.join(UPLOAD_FOLDER, filename)
 with open(fileFullPath, 'wb') as f:
 f.write(request.get_data())
 return jsonify({
 'filename': filename,
 'size': os.path.getsize(fileFullPath)
 })

script

The file test.jpg is in the same folder as this script. I tried to replicate the effect of curl -v -H 'Content-Type: application/octet-stream' -X POST --data @test.jpg localhost:5000/rupload/test.jpg using python code, as I dont want to use curl.

url='localhost:5000/rupload/test'
headers={'Content-Type':'application/octet-stream'}
requests.post(url,headers=headers,data='file.jpg')

When running this script (Flask application is runing as well) it throws me following error:

 Traceback (most recent call last):
 File "<path to my script.py>", line 10, in <module>
 requests.post(url,headers=headers,data='file.jpg')
 File "C:\Program Files\Python35\lib\site-packages\requests\api.py", line 112, in post
 return request('post', url, data=data, json=json, **kwargs)
 File "C:\Program Files\Python35\lib\site-packages\requests\api.py", line 58, in request
 return session.request(method=method, url=url, **kwargs)
 File "C:\Program Files\Python35\lib\site-packages\requests\sessions.py", line 508, in request
 resp = self.send(prep, **send_kwargs)
 File "C:\Program Files\Python35\lib\site-packages\requests\sessions.py", line 612, in send
 adapter = self.get_adapter(url=request.url)
 File "C:\Program Files\Python35\lib\site-packages\requests\sessions.py", line 703, in get_adapter
 raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for 'localhost:5000/rupload/test'

I'm not quite sure how to fix that problem. All approaches I tried so far didn't work. I guess the API is working correctly (it didn't recognize a request neither did it throw an error) and the error is solely caused by the script. All other parts of the API (not shown here) work correctly. It would be great if someone could point me to the direction on how to solve this issue.

Thanks

davidism
128k31 gold badges417 silver badges348 bronze badges
asked Aug 3, 2017 at 12:54
0

1 Answer 1

1

Use a properly qualified URL:

url='http://localhost:5000/rupload/test'
answered Aug 3, 2017 at 12:56
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.