In this article, I will briefly go over what an HTTP request is, as well as give an introductory example to facilitate its use in Python and outline its potential applications.
HTTP, Hypertext Transfer Protocol, is an application protocol utilized for data communication across the internet (www). An everyday example would be a user/client that attempts to logon to a website. Upon doing so, a TCP connection is initiated to the server with a GET request. The server then responds through the same port with a response code and text. There are also other request methods which express the intent of a client. For more please check this link.
GET/POST Requests
To demonstrate, lets go through a quick example to make HTTP GET/POST requests. Using Python, we will need the module requests. To install it, type the following in your terminal.
pip install requests
Now, in a Python terminal, try the following;
import requests
req = requests.get('http://google.com/')
print req
You should see the following ...
<Response [200]>
The returned objects is a the response object. The number 200 is utilized to describe that the action (our GET request) was successfully received and therefore, responded too. There are several other response codes that are used to describe client or server error, or even redirection or 'currently in process'. I won't go into more detail, I believe this link does a great job in saying all I want to say. To print the status code type ...
req.status_code
Moving along, we can also get a more descriptive response object by typing the following
print req.text
The text printed shows all the attributes of the response object, including the payload.
A more tangible example of a GET request would be to search a website like Amazon for example.
payload = {'field-keywords':'Breaking Bad'}
req = requests.get('https://amazon.com/s/', params=payload)
print req.url
The above print statement should give you a link, try navigating to that link in your favorite web browser and walaaahh! You've now searched Amazon using requests in Python. Hopefully that example gives you a better understanding of what a GET request is.
A simple, common use of a POST request is to fill in a form.
payload = {'key1': value1}
r = requests.post('https://signupform', params=payload)
The above code is hypothetical and will not return anything useful. It just serves to show you what a POST method does. I'm sure you will find it useful for your own application.
Handling Errors
A wiser, more maintainable approach while writing these requests is to take error handling and exceptions into consideration.
try:
req = requests.get('http://www.google.com/')
except requests.exceptions.RequestException as e:
print e.message
The above code is an example of how to make a GET request while handling a request exception error. In the snippet above, the handler catches the exception and prints the error message only. However, in your application, you can attempt several other actions after catching the exception. A common practice is to sleep for 10 seconds and re-attempt the request.