Andrex
Andrex

Reputation: 682

Upload a file using python requests

I have a problem trying to upload a file into an API. In the swagger UI I have not problem uploading an excel file manually. When I try tu upload using request, I recive a 415 error (Invalid format of my file). Here is a simple code of that post request:

 headers = {
        'Authorization':"bearer "+ CLIENT.token,
        'Content-Type': 'form-data' 
           }
 files = [('file', open(path_to_file, 'rb'))]

 response = requests.post(api_url,  
    files=files,
    headers=headers)

My response has status code 415, I dont Know what is happening. When I used the swagger, I inspected the newtwork browser, and I saw this header in the request

Content-Type: multipart/form-data; boundary=----WebKitFormBoundarywkrdGd3ROh0GkfsW

But, I don't know what is the term "boundary", and if I pass this header manually into the requests, the API throws a 500.

Upvotes: 0

Views: 1961

Answers (1)

Anonymous
Anonymous

Reputation: 12027

The server is saying that your Content-Type is wrong. If you're uploading a .xls file use:

'Content-Type': 'application/vnd.ms-excel'

If you're uploading a .xlsx file use:

'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

Upvotes: 1

Related Questions