0

I want to recreate this curl command using Python and requests:

curl --show-error --fail-early --user "user:pswd" --upload-file path_to_file\file.csv https://domain/REST/upload

This is a function I wrote to send the data:

# url = 'https://domain/REST/upload/file.csv'    <- includes file name

def upload_file(file: Path, url: str, user: str, pswd: str) -> None:
    with open(file, 'rb') as f:
        response = requests.put(url, files={file.name: f},
                                auth=HTTPBasicAuth(user, pswd))

The response I get:

response.status_code - 200
response.text - OK

However the delivered CSV has some lines added to it. To be more precise it looks like this:

-- d2bba62dkvj39bccd207b6d229c4f3f2                                  <- How do I get 
Content-Disposition: form-data; name="file.csv"; filename="file.csv" <- rid of these
                                                                     <- three lines?
NAME;  AGE; HEIGHT
Leslie;34;  157
Ron;   50;  179
Tom;   38;  168
April; 20;  168
                                                                     <- And also
-- d2bba62dkvj39bccd207b6d229c4f3f2                                  <- these two...
  • These aren't extra lines. This is the body of a FORM post or put (mutlipart/form-data) request. The server has to handle this or you need a different request that PUTs raw file data using the `data` parameter. When you use the `files` parameter to upload one or more files, `requests` creates a `multipart/form-data`-encoded body – Panagiotis Kanavos Jul 14 '23 at 08:17
  • 1
    Does this answer your question? [How do I use requests.put() to upload a file using Python?](https://stackoverflow.com/questions/47781633/how-do-i-use-requests-put-to-upload-a-file-using-python) – Panagiotis Kanavos Jul 14 '23 at 08:20
  • It seems, that the question you linked solves my problem. However I will have to wait until monday to have it confirmed. And thanks for the explanation! – user15634990 Jul 14 '23 at 08:51

0 Answers0