Wednesday, January 31, 2018

Web Basics - HTTP

I occasionally get asked to mentor by those interested in learning to code. My response and approach will vary depending on the situation. I've done this in a pair programming context, over the wire, and by simply offering advice from time to time. I enjoy mentoring, especially when the mentee shows commitment and drive. This blog was originally started on the basis of providing mentorship to my readers. With that, let me get back to my roots for this post.

If you want to learn web programming, you should probably start with the basics - HTTP. If you are mildly in IT (or have a pulse) you should understand the HTTP protocol to some extent. I'll start you on your journey to understanding this lifeblood of the internet.

http logo

What is it?

HTTP is a protocol for communication between two computers. In fact, it is an acronym for Hypertext Transfer Protocol. In HTTP, there is a request sent by a sender (client) to a receiver (server). And the server should send back a response.

The request has two parts, the headers and the body. The headers has information about the request and the body is where you put data you want to send to the server.

Verbs


There are different types of requests. Each type uses a different HTTP Verb (also called Method). Verbs are an important part of HTTP. They tell the server what you (the client) would like to do.

Verbs can be split into two categories - queries and commands. The most common are GET and POST.

GET is used to get a web page or some data. It is a query.

POST is widely used to do something, such as send an email or submit an order form. It is a command.

PUT is another command. PUT is meant for updating something. You might POST a file and later need to update the file's contents so you would PUT an update. There's also a DELETE command.

Another common query is OPTIONS. This is a request foe the server to tell you what you can do. Often servers will only accept certain verbs. OPTIONS can tell you which ones.

There are many others verbs. You can find more at the official w3 site at the end of this post.

Responses

When a request is made, the server can respond. One thing included in a response is a response code. A response code has 3 digits and ranges from 100-599. Each hundred has a different meaning. 100's are reserved, 200 level is success, 300 is content changed, 400 is a client error, 500 is server error.

Some common codes are 200 - OK, 204 - OK with no data returned, 302 - temporary redirect, 404 - not found, 500 - server error.

In addition to the response code, there may be other headers and content returned from the server. Some of the headers describe the content. For example, Content-Type and Content-Length. These tell your client (browser for instance) how to handle the content in the response. Should it display the content in a browser? Save it to a file? Open it in a plugin?

The Network

When we use the web, we are primarily using HTTP over TCP/IP (your next topic :) ). TCP is the transport mechanism/protocol (how) and IP is the addressing schema (where).

When you send an HTTP request, the server will need to know where and how to respond. Some of that information is in the request headers. And some of it is handled by the networking systems (everything that moves your request over the internet, from your client to the server).

Continued Learning

Most programming languages have HTTP clients that you can use is your programs. This is so common nowadays, that you cannot do programming for long without using an HTTP client. And when you do web programming, its at the core!

For example, web apps you use all the time use AJAX (Asynchronous JavaScript and XML) to make HTTP requests using the JavaScript code that the app runs in your browser! These requests do all sorts of things like fetch data for part of the page and send data to the server. When you see part of page spinning while it loads, that's usually the app waiting for a server to respond to an AJAX request!

You can find version 1.1 of the official protocol at rfc2616 

Learning HTTP is the start of your journey to understanding how the internet works. We'll explore TCP/IP in the future...its another core technology behind what the world runs on today.