0

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.

1
  • what is real url ? Do you have documentation for this API ? Maybe it needs something more. You can send it to http://httpbin.org/post in swagger UI and requests and you get information what did you send and then you can compare there information. Commented Dec 24, 2019 at 15:39

1 Answer 1

1

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'
Sign up to request clarification or add additional context in comments.

2 Comments

I tried to do that, but even doing I get the 415 status
@Andrex Try application/octet-stream

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.