1

I'm sending files to an API using python requests

    files = [("file", open(path_to_file, 'rb'))]
    headers = {"Authorization": "bearer " + CLIENT.token}
    response = requests.request(
        "POST",
        SETTINGS.URL_FILE_STORAGE,
        headers=headers,
        files=files
        # files= [('file',catalogo)],
    )

which throws a 202. But nevertheless, if I send the file as bytes, that is type(file) = Bytes The API throws a 500. What's the connection between send file data as open(path_file, 'rb') vs send data as Bytes?.

1 Answer 1

4

Which version of Requests are you using? In the current master branch at least there should be no difference. If you track down what happens to the files list in requests.request, you'll find that arguments of type str, bytes and bytearray get passed through as data, whereas for file-like objects, the .read() method is executed and the result of that is used instead.

So the following should produce the same request as your code:

    files = [("file", open(path_to_file, 'rb').read())] # Get file contents
    headers = {"Authorization": "bearer " + CLIENT.token}
    response = requests.request(
        "POST",
        SETTINGS.URL_FILE_STORAGE,
        headers=headers,
        files=files
        # files= [('file',catalogo)],
    )

I would be fairly surprised if the API responds differently to these two versions.

But if it does, and you have data of type bytes that you need to turn into a file-like object, you can simply use io:

import io
    files = [("file", io.BytesIO(data))]
    headers = {"Authorization": "bearer " + CLIENT.token}
    response = requests.request(
        "POST",
        SETTINGS.URL_FILE_STORAGE,
        headers=headers,
        files=files
        # files= [('file',catalogo)],
    )
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer!!, The problem was in the API, because when I was sending the file as bytes, requests tried to guess the file name (with its extension), in bytes mode the name was set as "file", and the API only supports specific extensions then it fell. I would have liked to receive another type of error, I dont know 415?. Thank you very much
Ah that makes sense. Glad you figured it out!
I'm confused: is there a typo in both code snippets? At line 0: It looks like you open a round bracket but forget to close it.
@HAL9000 Yes, that’s a typo that was copied from the question’s code. Should be a tuple inside a list.
ok, good: typo fixed

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.