Chapter 1. Interacting with the Web Using Requests

Reading data and obtaining information from web services tends to be a crucial task in these modern days. Everyone knows how an Application Programming Interface (API) allowed Facebook to spread the use of the Like button all over the Web and dominated the field of social communication. It has got its own flair to influence the business development, product development and supply chain management. At this stage, learning an efficient way to deal with the API's and opening the web URLs is the need of the hour. This will greatly affect many processes of web development.

Introduction to HTTP request

Whenever our Web browser tries communicating with a Web server, it is done by using the Hypertext Transfer Protocol (HTTP) which functions as a request-response protocol. In this process of communication, we send a request to the web server and expect a response in return. Take an example of downloading a PDF from a website. We send a request saying "Get me this specific file", and we get a response from the Web server with "Here is the file followed by the file itself". The HTTP request we are sending possibly has much interesting information. Let us dig inside it.

Here is the raw information of the HTTP request, that I have sent through my device. We can grasp the important parts of the request after looking at the following example:

* Connected to google.com (74.125.236.35) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: google.com
> Accept: */*
>
< HTTP/1.1 302 Found
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Location: http://www.google.co.in/?gfe_rd=cr&ei=_qMUVKLCIa3M8gewuoCYBQ
< Content-Length: 261
< Date: Sat, 13 Sep 2014 20:07:26 GMT
* Server GFE/2.0 is not blacklisted
< Server: GFE/2.0
< Alternate-Protocol: 80:quic,p=0.002

Now, we will send a request to the server. Let us make use of these parts of the HTTP request:

  • Method: The GET / http /1.1 in the preceding example, is the HTTP method which is case sensitive. Here are some of the HTTP request methods:
    • GET: This fetches information from the given server using the given URI.
    • HEAD: The functionality of this is similar to GET but the difference is, it delivers only the status line and header section.
    • POST: This can submit data to the server that we wish to process.
    • PUT: This creates or overwrites all the current representations of the target resource, when we intend to create a new URL.
    • DELETE: This removes all the resources that are described by the given Request-URI.
    • OPTIONS: This specifies the communication options for a request/response cycle. It lets the client to mention different options associated with the resource.
  • Request URI: Uniform Resource Identifier (URI) has the ability to recognize the name of the resource. In the previous example, the hostname is the Request-URI.
  • Request Header fields: If we want to add more information about the request, we can use the requests header fields. They are colon-separated key value pairs. Some of the request-headers values are:
    • Accept-Charset: This is used to indicate the character sets that are acceptable for the response.
    • Authorization: This contains the value of the credentials which has the authentication information of the user agent.
    • Host: This identifies the Internet host and port number of the resource that has been requested, using the original URI given by the user.
    • User-agent: It accommodates information about the user agent that originates the request. This can be used for statistical purposes such as tracing the protocol violations.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.30.19