Forums

Upload file not working

Hello, I'm trying to upload a file from my pc to my pythonanywhere folder. This is the code: :::python

test_file = open("database/conto.sqlite", "rb")
test_url = "https://www.pythonanywhere.com/user/[name_user]/files/home/[name_user]/mysite/database/"
test_response = requests.get(test_url, files = {"form_field_name": test_file})
if test_response.ok:
    print("Upload completed successfully!")
    print(test_response.text)
else:
    print("Something went wrong!")
    print(test_response.text)

The response si "Upload completed successfully!" but nothing has been uploaded! Where am I going wrong? Thank you!

You're not uploading -- you need to use a POST method, not GET. You could have a look at our CLI tool that might help you with the API, see: https://blog.pythonanywhere.com/202/.

Thank you, I will read it and will let you know! :)

I've made some progress, but still have some problems:

api_token = "my_token"
username = "my_username"
pythonanywhere_host = "www.pythonanywhere.com"

api_base = "https://{pythonanywhere_host}/api/v0/user/{username}/".format(
    pythonanywhere_host=pythonanywhere_host,
    username=username,
)
resp = requests.get(
    urljoin(api_base, "files/path/home/{username}/mysite/database/conto.sqlite".format(username=username)),
    headers={"Authorization": "Token {api_token}".format(api_token=api_token)}
)
print(resp.status_code)

The response is 200, the variable resp contains my data, but how can I dowload the file to my local directory? And when I try to upload the file the code is:

resp = requests.post(
    urljoin(api_base, "files/path/home/{username}/database/conto.sqlite".format(username=username)),
    files={"content":my_content},
    headers={"Authorization": "Token {api_token}".format(api_token=api_token)}
)

What should I put on 'content'? It's a sqlite file. Maybe I miss some knowledge! Thank you!

It's my fault, I realize that I didn't explain what is my purpose. I have a website where I enter data and save it in the database 'conto.sqlite' which is on the pythonanywhere server. I am creating an application with Pyqt5 that is supposed to insert more data and update the same file on the server. I think it's not possible to update directly a sqlite database so I wanted to download it when the application starts, insert my data and then upload it again when I have finished. Thank you!

No problem. Regarding your previous question -- the content, when you upload, should be in a multipart-encoded file, see docs. If by "download the file to your local directory" you meant saving it on your local machine, you need to perform extra steps that will do that, otherwise you will have the date stored only in the script's memory.

I tried:

resp = requests.post(
   urljoin(api_base, "files/path/home/{username}/database/conto.sqlite".format(username=username)),
   files={'content': 'multipart/form-data'},
   headers={"Authorization": "Token {api_token}".format(api_token=api_token)}
)

The resp.status_code is 200 but nothing is updated. I also tried:

resp = requests.post(
    urljoin(api_base, "files/path/home/{username}/database/conto.sqlite".format(username=username)),
    files={'content': open('database/conto.sqlite', 'rb')},
    headers={"Authorization": "Token {api_token}".format(api_token=api_token)}
)

but gives me the same result! :(

A 200 response means that a file has been updated; are you sure that the database file that you're uploading is actually being updated on the client side? You could, perhaps, try replacing it with a known different file, and then running just the upload code to see what happens.

YES! Now it works, this is the code that works for me:

resp = requests.post(
    urljoin(api_base, "files/path/home/{username}/mysite/database/conto.sqlite".format(username=username)),
    files={'content': open('database/conto.sqlite', 'rb')},
    headers={"Authorization": "Token {api_token}".format(api_token=api_token)}
)

In one of my many tries I forgot to put the directory 'mysite'!

Know I have to learn how to download my file so that I have not my date stored only in the script's memory!

Thank you very much!

Solution also to this second problem:

resp = requests.get(
    urljoin(api_base, "files/path/home/{username}/mysite/database/conto.sqlite".format(username=username)),
    headers={"Authorization": "Token {api_token}".format(api_token=api_token)}  
)
open('<folder>/conto.sqlite', 'wb').write(resp.content)

Thank you, you have been helpful! :)

Glad to hear that you made it work!