Slide 1

Slide 1 text

@PeterHilton http://hilton.org.uk/ HTTP APIs demystified Peeling back the top layer of the web application stack

Slide 2

Slide 2 text

Internet technology is like an onion: there’s always another layer. @PeterHilton • 2

Slide 3

Slide 3 text

A long time ago…

Slide 4

Slide 4 text

A long time ago… 1945 First description of a link-based information system. 1965 The term ‘hypertext’ coined for text containing links. 1970s Many hypertext computer systems implemented. 1980s The Internet becomes widely used by scientists. 1989 WorldWideWeb project introduces HTML to describe hypertext documents, and HTTP to fetch them via Internet. 1991 HTTP 0.9, 1996 HTTP 1.0, 1997-1999 HTTP 1.1 2015 HTTP 2 4 @PeterHilton •

Slide 5

Slide 5 text

HTTP = hypertext + Internet 5 @PeterHilton • The Internet is a communications network. HTTP - Hypertext Transfer Protocol is an Internet communication protocol. HTML - Hypertext Markeup Language is a text-based markup language for writing hypertext documents. Internet + HTTP + HTML = the web

Slide 6

Slide 6 text

HTTP in action

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Greeting

Hello, world!

hello.html

Slide 9

Slide 9 text

https://hilton.org.uk/hello.html URL Uniform Resource Locator (address)

Slide 10

Slide 10 text

https://hilton.org.uk/hello.html https scheme (transfer protocol) /hello.html (resource) URL path host (server) name http://hilton.org.uk

Slide 11

Slide 11 text

https://hilton.org.uk:3000/hello 80 is the default port in a URL port number

Slide 12

Slide 12 text

$ telnet www.x.com 80 Trying 160.153.63.10... Connected to x.com. Escape character is '^]'. GET / HTTP/1.1 Host: www.x.com HTTP/1.1 200 OK Date: Wed, 20 Jan 2021 09:13:04 GMT Server: Apache Upgrade: h2,h2c Connection: Upgrade Last-Modified: Fri, 14 Jul 2017 16:28:12 GMT ETag: "5a200a1-1-55449890c4e78" Accept-Ranges: bytes Content-Length: 1 Vary: User-Agent Content-Type: text/html x

Slide 13

Slide 13 text

Internet HTTP request HTTP response client server

Slide 14

Slide 14 text

$ telnet www.x.com 80 Trying 160.153.63.10... Connected to x.com. Escape character is '^]'. GET / HTTP/1.1 Host: www.x.com HTTP/1.1 200 OK Date: Wed, 20 Jan 2021 09:13:04 GMT Server: Apache Upgrade: h2,h2c Connection: Upgrade Last-Modified: Fri, 14 Jul 2017 16:28:12 GMT ETag: "5a200a1-1-55449890c4e78" Accept-Ranges: bytes Content-Length: 1 Vary: User-Agent Content-Type: text/html x } }response request

Slide 15

Slide 15 text

HTTP request in detail

Slide 16

Slide 16 text

GET /hello.html HTTP/1.1 Host: localhost:3000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36 Accept: text/html,application/xhtml+xml,application/ xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-GB,en;q=0.8,nl;q=0.6,fr;q=0.4 Cache-Control: max-age=0 Connection: keep-alive Upgrade-Insecure-Requests: 1

Slide 17

Slide 17 text

Form Send

Slide 18

Slide 18 text

POST /form HTTP/1.1 Host: localhost:3000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/601.6.17 (KHTML, like Gecko) Version/9.1.1 Safari/ 601.6.17 Content-Length: 24 Accept: text/html,application/xhtml+xml,application/ xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: en-us Connection: keep-alive Content-Type: application/x-www-form-urlencoded Dnt: 1 Origin: file:// colour=blue&fruit=banana

Slide 19

Slide 19 text

HTTP request methods GET - fetch a resource HEAD - returns the same headers as GET, but no body POST - send a resource PUT - replace a resource DELETE - delete a resource TRACE, OPTIONS, LINK, UNLINK, PATCH 19 @PeterHilton •

Slide 20

Slide 20 text

HTTP response in detail

Slide 21

Slide 21 text

HTTP/1.1 200 OK Server: GitHub.com Date: Sat, 16 Jul 2016 11:47:50 GMT Content-Type: text/html; charset=utf-8 Content-Length: 147 Last-Modified: Sat, 16 Jul 2016 08:27:20 GMT Access-Control-Allow-Origin: * Expires: Sat, 16 Jul 2016 11:57:50 GMT Cache-Control: max-age=600 Accept-Ranges: bytes X-GitHub-Request-Id: 53A0612B:15F4:2FDF3B:578A1EE6 Greeting

Hello, world!

Slide 22

Slide 22 text

HTTP/1.1 200 OK response status code status text

Slide 23

Slide 23 text

HTTP APIs

Slide 24

Slide 24 text

The other kind of application interface An application user interface (UI) lets you interact with an application by human (user) interaction. An application programming interface (API) lets you interact with an application by programming software to do so. 24 @PeterHilton •

Slide 25

Slide 25 text

‘Loading a web page’ HTTP/1.1 200 OK Server: GitHub.com Date: Sat, 16 Jul 2016 11:47:50 GMT Content-Type: text/html; charset=utf-8 Content-Length: 147 Cache-Control: max-age=600 Last-Modified: 16 Jul 2016 08:27:20 GMT Expires: Sat, 16 Jul 2016 11:57:50 GMT Access-Control-Allow-Origin: * Accept-Ranges: bytes Greeting

Hello, world!

‘Calling an API’ HTTP/1.1 200 OK Server: GitHub.com Date: Wed, 20 Jan 2021 09:38:36 GMT Content-Type: application/json; charset=utf-8 Content-Length: 78 Cache-Control: max-age=600 Last-Modified: Wed, 20 Jan 2021 09:38:36 GMT Expires: 20 Jan 2021 09:48:35 Access-Control-Allow-Origin: * Accept-Ranges: bytes { "message": { "content": "Hello, world!", "type": "Greeting" } }

Slide 26

Slide 26 text

‘Submitting a form on a web page’ POST /form HTTP/1.1 Content-Length: 24 Content-Type: application/ x-www-form-urlencoded colour=blue&fruit=banana ‘Calling an API’ POST /api HTTP/1.1 Content-Length: 78 Content-Type: application/json { "message": { "content": "Hello, world!", "type": "Greeting" } }

Slide 27

Slide 27 text

The tricky details

Slide 28

Slide 28 text

HTTP response status codes 28 @PeterHilton • 100-199 Informational 200-299 Client request successful 300-399 Request redirected, further action necessary 400-499 Client error - do not repeat the same request 500-599 Server error - maybe try again About 60 defined status codes, in the above five groups https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Slide 29

Slide 29 text

HTTP header fields (‘HTTP headers’) 29 @PeterHilton • General headers are used in both request and response Request headers indicate client preferences and config Response headers provide information about the server Entity headers describe the request or response body Non-standard headers are application-specific About 50 defined standard headers https://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Slide 30

Slide 30 text

Things that everyone gets wrong Character encodings URL encoding/decoding Caches and proxies Cookies, and using them to introduce ‘session’ state Documenting an API 30 @PeterHilton •

Slide 31

Slide 31 text

Further reading 31 @PeterHilton • Web Client Programming with Perl (O’Reilly, 1997), free online via O’Reilly Open Books, starts with three well- written chapters that teach everything web developers need to know about HTTP: Chapter 1: Introduction Chapter 2: Demystifying the Browser Chapter 3: Learning HTTP http://www.oreilly.com/openbook/webclient/

Slide 32

Slide 32 text

HTTP: The Definitive Guide 658 pages. 1035 grams. I have not read this book. Few developers need this much detail. 32 @PeterHilton •

Slide 33

Slide 33 text

@PeterHilton http://hilton.org.uk/