1

I have the following code:

    r = requests.put(
        config.get('webdav', 'url') + file_name,
        auth=(
            config.get('webdav', 'username'),
            config.get('webdav', 'password')
        ),
        files={
            "files": open(os.path.expanduser(charges_file_path), 'rb')
        }
    )

Which is fairly straightforward. It simply calls a PUT request to a webdav server, and pushes the data that is in files (plain text) to the server.

It works, except for a strange (or maybe not so strange if I am just missing something small) issue. When I do a GET on the file, or the file is viewed on the server directly, the file itself contains header information:

--55e72d74a10b423590cd4faa68212192 Content-Disposition: form-data; name="files"; filename="test_file6.txt"

(file_data)

--55e72d74a10b423590cd4faa68212192--

I haven't been able to find a reason or way around this. When I cURL the file from command line, it works fine.

Any ideas?

Community
  • 1
  • 1
MyCah
  • 408
  • 3
  • 8

1 Answers1

2

I am not really familiar with how Python requests works, but after reading through some docs and finding a similar issue someone had with sending files to Zendesk (this post), you might want to try using the data (or json) parameter instead of files in your request. Also, maybe attaching a params with filename if that's applicable here as well similar to the post I linked.

Another thing to do would be to put a Content-Type header on this request.

i.e.

requests.put(
  ...,
  headers={'Content-Type': 'application/binary'},
  data=open(os.path.expanduser(charges_file_path), 'rb').read()
)
Bagelfp
  • 61
  • 1
  • 7