-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
  • 121,510
  • 29
  • 395
  • 339
frameworker
  • 378
  • 3
  • 13

1 Answers1

1

Use a properly qualified URL:

url='http://localhost:5000/rupload/test'
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895