Python can be used to access webpages as well as post content to the webpages. There are various modules like httplib, urllib, httplib2 etc but the requests module is simplest and can be used to write simpler yet powerful programs involving GET and POST methods.
GET method
The GET method is part of the python requests module which is used to obtain data from a web URL. In the below example we reach out to our own website and find out various responses through the get method. We get the encoding, response time and also the header and part of the body.
Example
Live Demo
import requests req = requests.get('//www.tutorialspoint.com/') # Page encoding e = req.encoding print("Encoding: ",e) # Response code s = req.status_code print("Response code: ",s) # Response Time t = req.elapsed print("Response Time: ",t) t = req.headers['Content-Type'] print("Header: ",t) z = req.text print("\nSome text from the web page:\n",z[0:200])Output
Running the above code gives us the following result −
Encoding: UTF-8 Response code: 200 Response Time: 0:00:00.103850 Header: text/html; charset=UTF-8 Some text from the web page:POST Method
The POST method is used to send data mostly through a form to the server for creating or updating data in the server. The requests module provides us with post method which can directly send the data by taking the URL and value of the data parameter.
In the below example we post some data to the httpbin.org website through the post method and get a response on how it is posted.
Example
Live Demo
import requests in_values = {'username':'Jack','password':'Hello'} res = requests.post('//httpbin.org/post',data = in_values) print(res.text)Output
Running the above code gives us the following result −
{ "args": {}, "data": "", "files": {}, "form": { "password": "Hello", "username": "Jack" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "28", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.22.0", "X-Amzn-Trace-Id": "Root=1-5ef75488-969f97a68bb72642b97b6d50" }, "json": null, "origin": "122.xxx.yy.zzz", "url": "//httpbin.org/post" }
Updated on 10-Jul-2020 10:59:24
- Related Questions & Answers
- What is the difference between GET and POST in Python CGI Programming?
- PHP – Parse the GET, POST, and COOKIE data using mb_parse_str()
- Difference Between GET and POST Method in HTML
- Passing Information Using POST Method in Python
- Using POST Methods in Perl
- What is difference between GET and POST method in HTTP protocol?
- Image based Steganography using Python Programming
- Twitter Sentiment Analysis using Python Programming.
- Python Program to Implement Depth First Search Traversal using Post Order
- Routing requests in Node.js
- Redirecting requests in Node.js
- Post favourite stuffs using pixelpumper on mac
- Using post request in middleware in express
- How to get the POST values from serializeArray in PHP?
- Parsing incoming requests in express.js
❮ Requests Module
Example
Make a POST request to a web page, and return the response text:
import requests
url = '//www.w3schools.com/python/demopage.php'
myobj = {'somekey': 'somevalue'}
x = requests.post(url, json = myobj)
print(x.text)
Run Example »
Definition and Usage
The post() method sends a POST request to the specified url.
The post() method is used when you want to send some data to the server.
Syntax
requests.post(url, data={key: value}, json={key: value}, args)
args means zero or more of the named arguments in the parameter table below. Example:
requests.post(url, data = myobj, timeout=2.50)
Parameter Values
url | Try it | Required. The url of the request |
data | Try it | Optional. A dictionary, list of tuples, bytes or a file object to send to the specified url |
json | Try it | Optional. A JSON object to send to the specified url |
files | Try it | Optional. A dictionary of files to send to the specified url |
allow_redirects | Try it | Optional. A Boolean to enable/disable redirection. Default True (allowing redirects) |
auth | Try it | Optional. A tuple to enable a certain HTTP authentication. Default None |
cert | Try it | Optional. A String or Tuple specifying a cert file or key. Default None |
cookies | Try it | Optional. A dictionary of cookies to send to the specified url. Default None |
headers | Try it | Optional. A dictionary of HTTP headers to send to the specified url. Default None |
proxies | Try it | Optional. A dictionary of the protocol to the proxy url. Default None |
stream | Try it | Optional. A Boolean indication if the response should be immediately downloaded (False) or streamed (True). Default False |
timeout | Try it | Optional. A number, or a tuple, indicating how many seconds to wait for the client to make a connection and/or send a response. Default None which means the request will continue until the connection is closed |
verify | Try it Try it | Optional. A Boolean or a String indication to verify the servers TLS certificate or not. Default True |
Return Value
A requests.Response object.
❮ Requests Module