Post files with requests

Posted on 2014-03-21 00:00:00 -0800

AKA

1
requests.post(files=?)

Today I was attempting to POST some data to Docverter’s API with the python requests library, and ran into a problem. Although

1
requests.post
supports a files keyword argument, it does not seem to support custom, per file, Content-Type headers.

However, trawling through the source code, it would seem that yes, yes, it does. It’s just not documented. It was initially added in a commit over a year ago, but alas, the commit did not include documentation.

It seems the following are possible, where ellipses indicate the inclusion of every argument of the above example;

Albeit good that these different forms are supported, ideally all forms should be documented, not just the first.

N.B; now that I know what to look for, the Content-Type form is also mentioned in a Stackoverflow answer, and in the urllib3 documentation (though it fails to mention the custom headers)