Documentation for the Python Requests module in the context of the HTTP POST method and its corresponding Requests function
Simple HTTP Authentication
Simple HTTP Authentication can be achieved with the following:
This is technically short hand for the following:
HTTP Digest Authentication
HTTP Digest Authentication is done in a very similar way, Requests provides a different object for this:
In some cases the built in authentication mechanisms may not be enough, imagine this example:
A server is configured to accept authentication if the sender has the correct user-agent string, a certain header value and supplies the correct credentials through HTTP Basic Authentication. To achieve this a custom authentication class should be prepared, subclassing AuthBase, which is the base for Requests authentication implementations:
This can then be utilized with the following code:
With the Requests module,its is only necessary to provide a file handle as opposed to the contents retrieved with .read():
Filename, content_type and headers can also be set:
Strings can also be sent as a file, as long they are supplied as the files parameter.
Multiple files can be supplied in much the same way as one file:
Form Encoded Data
To pass form encoded data with the post operation, data must be structured as dictionary and supplied as the data parameter.
If the data does not want to be form encoded, simply pass a string, or integer to the data parameter.
Supply the dictionary to the json parameter for Requests to format the data automatically:
Each request POST operation can be configured to use network proxies
HTTP Basic Authentication can be provided in this manner:
The use of socks proxies requires 3rd party dependencies requests[socks], once installed socks proxies are used in a very similar way to HTTPBasicAuth:
Response codes can be viewed from a post operation:
Accessing data that is returned:
In the instances where you need to access the underlying urllib3 response.HTTPResponse object, this can be done by the following:
Will perform a simple HTTP POST operation. Posted data can be inmost formats, however key value pairs are most prevalent.
Headers can be viewed:
An example response:
Headers can also be prepared before post:
Encoding can be set and viewed in much the same way:
Requests by default validates SSL certificates of domains. This can be overridden:
Any redirection will be followed (e.g. http to https) this can also be changed:
If the post operation has been redirected, this value can be accessed:
A full history of redirects can be viewed:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0