You are on page 1of 33

HTTP Basics

HTTP Request & HTTP Response

SoftUni Team
Technical Trainers
Software University
https://softuni.bg
Table of Contents
1. HTTP Basics
2. HTML Forms
3. HTTP Request
4. HTTP Response
5. URL

2
Have a Question?

sli.do
#fund-common
3
http://
HTTP Basics
HTTP Basics
 HTTP (HyperText Transfer Protocol)
 Text-based client-server protocol for the Internet
 For transferring Web resources (HTML files, images, styles, etc.)
 Request-response based

HTTP request

HTTP response
Web Client Web Server
5
Web Server Work Model
Web Client Web Server Technology

Request

Response

Web Resources
Database

HTML, PDF, JPG…


6
Hyper Text Transfer Protocol

Request Response

HTTP HTTP
TCP TCP
IP IP
Media
Ethernet Ethernet
(wires / air /
fiber)
7
HTTP Request Methods
HTTP defines methods to indicate the desired action to be
performed on the identified resource
Method Description Other HTTP Methods
POST Create / store a resource CONNECT
GET Read / retrieve a resource HEAD
PUT Update / modify a resource OPTIONS
DELETE Delete / remove a resource TRACE

The four basic functions of persistence storage.


8
Dev Tools
HTTP Developer Tools

Developer Tools Postman


10
HTML Forms
HTML Forms – Action Attribute
 Defines where to submit the form data: Relative URL to the
current file
<form action="home.html">
<input type="submit" value="Go to homepage"/>
</form>

12
HTML Forms – Method Attribute
 Specifies the HTTP method to use when sending form data
<form method="get">
Name: <input type="text" name="name">
<br /><br />
<input type="submit" value="Submit">
The form data
</form> is in the URL

13
HTML Forms – Method Attribute (2)
<form method="post">
Name: <input type="text" name="name">
<br /><br />
<input type="submit" value="Submit">
</form>

POST http://localhost/index.html HTTP/1.1


Headers will be
Host: localhost
explained later
Content-Type: application/x-www-form-urlencoded
Content-Length: 10

name=Pesho HTTP request body holds the request


form data and the response data
14
URL Encoded Form Data – Example
<form method="post">
Name: <input type="text" name="name"/> <br/>
Age: <input type="text" name="age"/> <br/>
<input type="submit" />
</form>

POST http://localhost/cgi-bin/index.cgi HTTP/1.1


Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
File uploads are
name=Maria+Smith&age=19
not supported

15
HTTP Request
HTTP Request Methods
 HTTP defines methods to indicate the desired action to be
performed on the identified resource
Method Description
GET Retrieve / load a resource
POST Create / store a resource
PUT Update a resource
DELETE Delete (remove) a resource
PATCH Update resource partially
HEAD Retrieve the resource's headers

17
HTTP GET Request – Example
GET /users/SoftUni-Tech-Module/repos HTTP/1.1
Host: api.github.com HTTP request line
Accept: */* HTTP headers
Accept-Language: en
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64)
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/54.0.2840.71 Safari/537.36
Connection: Keep-Alive
Cache-Control: no-cache The request
body is empty
<CRLF>
18
HTTP POST Request – Example
POST /repos/Tech-Module-Jan-2018/test-repo/issues HTTP/1.1
Host: api.github.com
HTTP headers HTTP request line
Accept: */*
Accept-Language: en
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible;MSIE 6.0; Windows NT 5.0)
Connection: Keep-Alive
Cache-Control: no-cache
<CRLF> The request body holds
the submitted data
{"title":"Found a bug",
"body":"I'm having a problem with this.",
"labels":["bug","minor"]}
<CRLF>
19
HTTP Response
HTTP Response – Example
HTTP/1.1 200 OK HTTP response status line
Date: Fri, 11 Nov 2016 16:09:18 GMT+2
Server: Apache/2.2.14 (Linux)
Accept-Ranges: bytes
Content-Length: 84 HTTP response
Content-Type: text/html headers
<CRLF>
<html>
<head><title>Test</title></head> HTTP response
<body>Test HTML page.</body> body
</html>
21
HTTP Response Status Codes
Status Code Action Description
200 OK Successfully retrieved resource
201 Created A new resource was created
204 No Content Request has nothing to return
301 / 302 Moved Moved to another location (redirect)
400 Bad Request Invalid request / syntax error
401 / 403 Unautho- Authentication failed / Access denied
rized
404 Not Found Invalid resource
409 Conflict Conflict was detected, e.g. duplicated email
500 / 503 Server Error Internal server error / Service unavailable

22
Content-Type and Disposition
 The Content-Type / Content-Disposition headers
specify how the HTTP request / response body should be
processed
 Examples: JSON-encoded data
UTF-8 encoded HTML page.
Content-Type: application/json Will be shown in the browser
Content-Type: text/html; charset=utf-8

Content-Type: application/pdf
Content-Disposition: attachment; filename="Financial-
Report-April-2016.pdf" This will download a PDF file named
Financial-Report-April-2016.pdf 23
HTTP Conversation: Example

GET /trainings/courses HTTP/1.1


Host: www.softuni.bg
 HTTP request: User-Agent: Mozilla/5.0
<CRLF>
The empty line denotes the
end of the request header
HTTP/1.1 200 OK
Date: Tue, 16 Jan 2018 15:13:41 GMT
Server: Microsoft-HTTPAPI/2.0
Last-Modified: Tue, 16 Jan 2018 15:13:42 GMT
 HTTP response: Content-Length: 18586
<CRLF> The empty line denotes the
<html><title>Курсовете от… end of the response header
</title>
24
URL
Uniform Resource Locator (URL)
http://mysite.com:8080/demo/index.php?id=27&lang=en#lectures

Protocol Host Port Path Query Fragment


String
 Protocol for communicating (http, ftp, https...) – HTTP in most cases
 Host or IP address (www.softuni.bg, gmail.com, 127.0.0.1, web)
 Port (the default port is 80) – a number in range [0…65535]
 Path (/forum, /path/index.php)
 Query string (?id=27&lang=en)
 Fragment (#lectures) – used on the client to navigate to some section
26
Query String
 Query string contains data that is not part of the path structure
 Commonly used in searches and dynamic pages
 It is the part of a URL following a question mark (?)
 Multiple parameters are separated by some delimiter
http://example.com/path/to/page?name=ferret&color=purple

27
URL Encoding
 URLs are encoded according RFC 1738:
 Unreserved URL characters – have no special meaning
[0-9a-zA-Z]

 Reserved URL characters – can have a special meaning in the URL


! * ' ( ) ; : @ & = + $ / , ? # [ ]

 Reserved characters are escaped by percent encoding


%[character hex code]
 Space is encoded as "+" or "%20"

28
URL Encoding – Examples
 All other characters are escaped by %
Char URL Encoding
space %20
щ %D1%89
" %22
# %23
$ %24
Each character is
converted to its ASCII
% %25
value, represented as
& %26
hexadecimal digits

Наков- 爱 -SoftUni %D0%9D%D0%B0%D0%BA%D0%BE%D0%B2-%E7%88%B1-SoftUni

29
Valid and Invalid URLs – Examples
 Some valid URLs:
http://www.google.bg/search?sourceid=navclient&ie=UTF-
8&rlz=1T4GGLL_enBG369BG369&q=http+get+vs+post

http://bg.wikipedia.org/wiki/%D0%A1%D0%BE
%D1%84%D1%82%D1%83%D0%B5%D1%80%D0%BD%D0%B0_%D0%B0%D0%BA
%D0%B0%D0%B4%D0%B5%D0%BC%D0%B8%D1%8F
Should be:
 Some invalid URLs: C%23+.NET+4.0

http://www.google.bg/search?&q=C# .NET 4.0

Should be: %D0%B1


http://www.google.bg/search?&q=бира
%D0%B8%D1%80%D0%B0
30
Summary

 HTTP
… works with message pairs
HTTP request and HTTP response
 …
GET /trainings/courses HTTP/1.1 HTTP/1.1 200 OK
 …www.softuni.bg
Host: …
User-Agent: Mozilla/5.0 <CRLF>
<CRLF> <html><title>…

 The URL parts define:


protocol, host, port, path, query string, and
fragment

31
Questions?

© SoftUni – https://softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Trainings @ Software University (SoftUni)
 Software University – High-Quality Education,
Profession and Job for Software Developers
 softuni.bg
 Software University Foundation
 softuni.foundation
 Software University @ Facebook
 facebook.com/SoftwareUniversity
 Software University Forums
 forum.softuni.bg
33

You might also like