We will discuss Web Services and related concepts in this section. We start with some key terminology:
HTTP stands for HyperText Transfer Protocol. It is what is known as an "Application Layer Networking protocol". This means that it does not concern itself with how network packets will traverse the internet, but instead aims at a higher-level description of the information in those packets. It is the primary mode of communication between web browsers and web servers. Every time you request a webpage, HTTP is the protocol that arranges for that webpage to find its way to you.
There are some key characteristics that determine the behavior of HTTP:
Every resource you may want to access has a corresponding Uniform Resource Identifier (URI), what we all affectionately know as a URL. A simple, clear, uniquely identifiable access point for the resource. URIs have the general form (example taken from Wikipedia):
scheme:[//authority]path[?query][#fragment]
https://www.example.com:123/forum/questions/?tag=networking&order=newest
where the first part is the protocol used, followed by the internet name for the server we are trying to access, typically called the hostname, optionally including a port number (123 in the example above). This is then followed by the path to the resource we want to reach. A question mark indicates the beginning of a query component which contains extra parameters for the request.
The protocol is "stateless". In other words, each request that the client sends to the server has no memory of prior requests and replies.
This is an important feature of the protocol, and something that actually lent to its popularity. It makes the implementation of it on both the server and client side easier, and makes the overall protocol simpler, as neither side needs to maintain any information from previous requests. Other protocols had been proposed around the same time, but HTTP won in the end as the standard, in large part due to its simplicity.
One of the consequences of course is that in situations where we need to maintain the history of what has occured, both browser and server need to agree on a way to do that (e.g. keeping someone "logged in").
The typical interaction between client and server, called a session, would go something like this:
Clients and servers specify themselves via their IP addresses (and port numbers). This is taken care of at the TCP/IP layer.
The client sends an "HTTP request" to the server over the network. That request includes the request method, followed by the resource path as well as the protocol version, typically HTTP/1.1.
It will be followed by a series of headers, that can identify various aspects of the request, like the content type, the host name, the content length, the accepted languages for the reply and so on.
Some request methods allow extra content, which can be found below the headers. Here is an example from the MDN page:
POST /contact_form.php HTTP/1.1
Host: developer.mozilla.org
Content-Length: 64
Content-Type: application/x-www-form-urlencoded
name=Joe%20User&request=Send%20me%20one%20of%20your%20catalogue
This says that the request uses the POST method and the resource path is /contact_form.php
. There are 3 headers, one specifying the host, and two more specifying the details about the content. After that and following an empty line we find the content (name...
).
Here are the main request methods. Think of these as "function calls". A web browser can typically only use the first two, but the client of a web service could use more.
Server responses have a special first line containing the protocol followed by the response status code. This is followed by response headers, and finally the content body of the response. Here is an example:
HTTP/1.1 200 OK
Date: Sat, 09 Oct 2010 14:28:02 GMT
Server: Apache
Last-Modified: Tue, 01 Dec 2009 20:18:22 GMT
ETag: "51142bc1-7449-479b075b2891b"
Accept-Ranges: bytes
Content-Length: 29769
Content-Type: text/html
<!DOCTYPE html... (here comes the 29769 bytes of the requested web page)
There are many different request and response headers, look at the documentation for more information.
Here are some typical response status codes, there are many more:
Location
header gives the new location. In general, all codes in the 3xx range indicate some sort of redirection.
You typically don't have to directly create these requests and responses as text, there are libraries that do that for you and allow you to talk about these responses on the level of objects.
In this activity one student acts as the server, while other students act as clients making HTTP requests of the server. The server is required to send a response for each request. Here are the instructions for the server:
/add
and /subtract
./minus
, but since the creation of /subtract
you want to encourage all your users to transition to it, by providing a redirect.Content-Length
header. If that header is not present, you should return a suitable status code.Content-Length
header value depending on the size of the result.