Package: requests-toolbelt
The Requests ToolbeltThis is just a collection of utilities forpython-requests, but don’t
really belong inrequestsproper. The minimum tested requests version is2.1.0. In reality, the toolbelt should work with2.0.1as well, but
some idiosyncracies prevent effective or sane testing on that version.pip installrequests-toolbeltto get started!multipart/form-data EncoderThe main attraction is a streaming multipart form-data object,MultipartEncoder.
Its API looks like this:fromrequests_toolbeltimportMultipartEncoderimportrequestsm=MultipartEncoder(fields={'field0':'value','field1':'value','field2':('filename',open('file.py','rb'),'text/plain')})r=requests.post('http://httpbin.org/post',data=m,headers={'Content-Type':m.content_type})You can also usemultipart/form-dataencoding for requests that don’t
require files:fromrequests_toolbeltimportMultipartEncoderimportrequestsm=MultipartEncoder(fields={'field0':'value','field1':'value'})r=requests.post('http://httpbin.org/post',data=m,headers={'Content-Type':m.content_type})Or, you can just create the string and examine the data:# Assuming `m` is one of the abovem.to_string()# Always returns unicodeUser-Agent constructorYou can easily construct a requests-styleUser-Agentstring:from requests_toolbelt import user_agent

headers = {
    'User-Agent': user_agent('my_package', '0.0.1')
    }

r = requests.get('https://api.github.com/users', headers=headers)SSLAdapterTheSSLAdapterwas originally published onCory Benfield’s blog.
This adapter allows the user to choose one of the SSL protocols made available
in Python’ssslmodule for outgoing HTTPS connections:fromrequests_toolbeltimportSSLAdapterimportrequestsimportssls=requests.Session()s.mount('https://',SSLAdapter(ssl.PROTOCOL_TLSv1))cookies/ForgetfulCookieJarTheForgetfulCookieJarprevents a particular requests session from storing
cookies:fromrequests_toolbelt.cookies.forgetfulimportForgetfulCookieJarsession=requests.Session()session.cookies=ForgetfulCookieJar()ContributingPlease read thesuggested workflowfor
contributing to this project.Please report any bugs on theissue trackerHistory1.0.0 – 2023-05-01Breaking ChangesRemoved Google App Engine support to allow using urllib3 2.0Fixed BugsEnsured the test suite no longer reaches the InternetMiscellaneousAdded explicit support for Python 3.110.10.1 – 2022-10-25Fixed BugsFix urllib3 warning to only emit on X509Adapter usage0.10.0 – 2022-10-06New FeaturesAdd support for preparing requests in BaseUrlSessionFixed BugsFixing missing newline in dump utility0.9.1 – 2019-01-29Fixed BugsFix import of pyOpenSSL shim from urllib3 for PKCS12 adapter0.9.0 – 2019-01-29New FeaturesAdd X509 Adapter that can handle PKCS12Add stateless solution for streaming files by MultipartEncoder from one host to another (in chunks)Fixed BugsUpdate link to exampleMove import ofABCsfrom collections into version-specific part of
_compat moduleFix backwards incompatibility inget_encodings_from_contentCorrect callback documentation forMultipartEncoderMonitorFix bug whenMultipartEncoderis asked to encode zero partsCorrect the type of non string request body dumpsRemoved content from being stored in MultipartDecoderFix bug by enabling support for contenttype with capital letters.Coerce proxy URL to bytes before dumping requestAvoid bailing out with exception upon empty response reasonCorrected Pool documentationCorrected parentheses match in example usageFix “oject” to “object” inMultipartEncoderFix URL for the project after the moveAdd fix for OSX TCPKeepAliveAdapterMiscellaneousRemove py33 from testing and add Python 3.6 and nightly testing to the travis matrix.0.8.0 – 2017-05-20More information about this release can be found on the0.8.0 milestone.New FeaturesAddUserAgentBuilderto provide more control over generated User-Agent
strings.Fixed BugsInclude_validate_certificatein the lits of picked attributes on theAppEngineAdapter.Fix backwards incompatibility inget_encodings_from_content0.7.1 – 2017-02-13More information about this release can be found on the0.7.1 milestone.Fixed BugsFixed monkey-patching for the AppEngineAdapter.Make it easier to disable certificate verification when monkey-patching
AppEngine.Handlemultipart/form-databodies without a trailingCRLF.0.7.0 – 2016-07-21More information about this release can be found on the0.7.0 milestone.New FeaturesAddBaseUrlSessionto allow developers to have a session that has a
“Base” URL. See the documentation for more details and examples.Split the logic ofstream_response_to_fileinto two separate functions:get_download_file_pathto generate the file name from the Response.stream_response_to_filewhich will useget_download_file_pathif
necessaryFixed BugsFixed the issue for people usingveryold versions of Requests where they
would see an ImportError fromrequests_toolbelt._compatwhen trying to
importconnection.0.6.2 – 2016-05-10Fixed BugsWhen passing a timeout via Requests, it was not appropriately translated to
the timeout that the urllib3 code was expecting.0.6.1 – 2016-05-05Fixed BugsRemove assertion about request URLs in the AppEngineAdapter.Prevent pip from installing requests 3.0.0 when that is released until we
are ready to handle it.0.6.0 – 2016-01-27More information about this release can be found on the0.6.0 milestone.New FeaturesAddAppEngineAdapterto support developers using Google’s AppEngine
platform with Requests.AddGuessProxyAuthclass to support guessing between Basic and Digest
Authentication for proxies.Fixed BugsEnsure that proxies use the correct TLS version when using theSSLAdapter.Fix anAttributeErrorwhen using theHTTPProxyDigestAuthclass.MiscellaneousDrop testing support for Python 3.2. virtualenv and pip have stopped
supporting it meaning that it is harder to test for this with our CI
infrastructure. Moving forward we will make a best-effort attempt to
support 3.2 but will not test for it.0.5.1 – 2015-12-16More information about this release can be found on the0.5.1 milestone.Fixed BugsNow papers over the differences in requests’super_lenfunction from
versions prior to 2.9.0 and versions 2.9.0 and later.0.5.0 – 2015-11-24More information about this release can be found on themilestonefor 0.5.0.New FeaturesTheteesubmodule was added torequests_toolbelt.downloadutils. It
allows you to iterate over the bytes of a response while also writing them
to a file. Thetee.teefunction, expects you to pass an open file
object, whiletee.tee_to_filewill use the provided file name to open
the file for you.Added a new parameter torequests_toolbelt.utils.user_agentthat allows
the user to specify additional items.Added nested form-data helper,requests_toolbelt.utils.formdata.urlencode.Added theForgetfulCookieJartorequests_toolbelt.cookies.Added utilities for dumping the information about a request-response cycle
inrequests_toolbelt.utils.dump.Implemented the API described in therequests_toolbelt.threadedmodule
docstring, i.e., addedrequests_toolbelt.threaded.mapas an available
function.Fixed BugsNow papers over the API differences in versions of requests installed from
system packages versus versions of requests installed from PyPI.Allow string types forSourceAddressAdapter.0.4.0 – 2015-04-03For more information about this release, please seemilestone 0.4.0on the project’s page.New FeaturesA naive implemenation of a thread pool is now included in the toolbelt. See
the docs indocs/threading.rstor onRead The Docs.TheStreamingIteratornow accepts files (such assys.stdin) without
a specific length and will properly stream them.TheMultipartEncodernow accepts exactly the same format of fields as
requests’filesparameter does. In other words, you can now also pass in
extra headers to add to a part in the body. You can also now specify a
customContent-Typefor a part.An implementation of HTTP Digest Authentication for Proxies is now included.A transport adapter that allows a user to specify a specific Certificate
Fingerprint is now included in the toolbelt.A transport adapter that simplifies how users specify socket options is now
included.A transport adapter that simplifies how users can specify TCP Keep-Alive
options is now included in the toolbelt.Deprecated functions fromrequests.utilsare now included and
maintained.An authentication tool that allows users to specify how to authenticate to
several different domains at once is now included.A function to save streamed responses to disk by analyzing theContent-Dispositionheader is now included in the toolbelt.Fixed BugsTheMultipartEncoderwill now allow users to upload files larger than
4GB on 32-bit systems.TheMultipartEncoderwill now accept empty unicode strings for form
values.0.3.1 – 2014-06-23Fix the fact that 0.3.0 bundle did not include theStreamingIterator0.3.0 – 2014-05-21Bug FixesComplete rewrite ofMultipartEncoderfixes bug where bytes were lost in
uploadsNew FeaturesMultipartDecoderto acceptmultipart/form-dataresponse bodies and
parse them into an easy to use object.SourceAddressAdapterto allow users to choose a local address to bind
connections to.GuessAuthwhich accepts a username and password and uses theWWW-Authenticateheader to determine how to authenticate against a
server.MultipartEncoderMonitorwraps an instance of theMultipartEncoderand keeps track of how many bytes were read and will call the provided
callback.StreamingIteratorwill wrap an iterator and stream the upload instead of
chunk it, provided you also provide the length of the content you wish to
upload.0.2.0 – 2014-02-24Add ability to tellMultipartEncoderwhich encoding to use. By default
it uses ‘utf-8’.Fix #10 - allow users to install with pipFix #9 - FixMultipartEncoder#to_stringso that it properly handles file
objects as fields0.1.2 – 2014-01-19At some point during development we broke how we handle normal file objects.
Thanks to @konomae this is now fixed.0.1.1 – 2014-01-19Handleio.BytesIO-like objects better0.1.0 – 2014-01-18Add initial implementation of the streamingMultipartEncoderAdd initial implementation of theuser_agentfunctionAdd theSSLAdapter