As you know,
there are a number of different kinds of HTTP requests that you can make to
perform different kinds of actions on a URL resource.
When you access a particular target resource that is a web server,
not all request types might be supported.
In order for you to know what request types are supported,
you can make a request to that URL with the OPTIONS method.
The response that we get for the OPTIONS method describes the
communications options that we have for this particular URL.
You can access this URL using GET, POST, PUT, DELETE, PATCH and OPTIONS methods.
When you access a particular resource,
if you want just header information to be returned,
but you don't want the response body, you can use the method HEAD.
A HEAD request is identical to a GET to retrieve information,
but this does not retrieve data.
That is, there is no data in the response body.
You'll only get the headers.
When you don't specify an explicit method to access the target resource,
the default method for the http.request function is the GET method.
You can explicitly indicate the GET method as well.
The httpbin.org has a /get part that supports GET requests explicitly.
We made a request to a slightly different URL.
The content-location header has a different value.
And observe that the content-type specified here is application/json,
meaning that our response content is in the JSON format.
The GET request that we worked with so far is used to
retrieve information from a server.
POST requests are what you'll use to send data to a
server to update a server resource.
Here is some simple POST data that I want to send to a server,
specified in the form of a JSON string.
The same http.request function can be used to make POST requests as well.
We'll specify the URL to which you want to send this POST request.
You won't be able to make POST requests to any URL out
there on the web without authentication.
But with httpbin.org /post, you can test out your POST requests like we do here.
Specify the body of the request that you want to send
to the server in the body parameter.
We also specify within our headers that our content type
is in the application/json mime type.
The response variable contains the headers that we receive in
response to the POST request that we just made.
Let's take a look at the response body that we received from httpbin.org,
and you can see that this site simply sends our data
back to us in the response body.
Observe that the response is in the JSON format.
And the data key,
as well as the JSON key within this JSON structure just returns the
same POST data that we'd submitted to the server.
This is extremely useful when we want to test our programs
out with specific kinds of responses,
which is why we've used the site in the first place.
Here is a header field that is interesting, the User-Agent.
The User-Agent is simply a string that identifies how
this request was made to the resource.
If the request was made via a web browser,
the User-Agent will contain browser information,
as well as your operating system information.
Our request was made using Python, specifically the httplib2 library,
and you can see that the User-Agent has identified this.
If you want to submit data to a server or update the resource on the server,
another option for you is the PUT request.
PUTs are similar to POSTs except that PUT requests are idempotent,
which means you can invoke the same PUT request multiple
times; it will always produce the same result.
Make your PUT request to httpbin.org /put,
and the content that we pass in our request body is the same JSON
content that we had sent to the server earlier.
In this manner, when we use the httplib2 library,
the same http.request function can be used for different
kinds of invocations on the target resource.