Fsa CS4.1 HTTP
Fsa CS4.1 HTTP
Development
• The request headers are in the form of name:value pairs. Multiple values,
separated by commas, can be specified.
GET
The GET method requests a representation of the specified resource. Requests using GET should only
retrieve data.
The GET request method is said to be a safe operation, which means it should not change the state of
any resource on the server.
The GET method is used to request any of the following resources:
• A webpage or HTML file.
• An image or video.
• A JSON document.
• A CSS file or JavaScript file.
• An XML file.
HTTP Methods
POST:
The POST method submits an entity to the specified resource, often causing a change in state or side effects
on the server.
The POST HTTP request method sends data to the server for processing.
The data sent to the server is typically in the following form:
• Input fields from online forms.
• XML or JSON data.
• Text data from query parameters.
A POST operation is not considered a safe operation, as it has the power to update the state of the server and
cause potential side effects to the server’s state when executed.
The HTTP POST method is not required to be idempotent either, which means it can leave data and resources
on the server in a different state each time it is invoked.
HTTP Methods
HEAD
• The HEAD method requests a response identical to a GET request but without the response body.
• The HTTP HEAD method returns metadata about a resource on the server.
• The HTTP HEAD method is commonly used to check the following conditions:
• The size of a resource on the server.
• If a resource exists on the server or not.
• The last-modified date of a resource.
• Validity of a cached resource on the server.
• The following example shows sample data returned from a HEAD request:
HTTP/1.1 200 OK
Date: Fri, 19 Aug 2023 12:00:00 GMT
Content-Type: text/html
Content-Length: 1234
Last-Modified: Thu, 18 Aug 2023 15:30:00 GMT
HTTP Methods
• PUT
• The HTTP PUT method is used to replace a resource identified with a given URL completely.
• The HTTP PUT request method includes two rules:
• A PUT operation always includes a payload that describes a completely new resource definition to be
saved by the server.
• The PUT operation uses the exact URL of the target resource.
• If a resource exists at the URL provided by a PUT operation, the resource’s representation is completely
replaced.
• A new resource is created if a resource does not exist at that URL.
• The payload of a PUT operation can be anything that the server understands, although JSON and XML
are the most common data exchange formats for RESTful web services.
HTTP Methods
DELETE
The DELETE method deletes the specified resource.
CONNECT
The CONNECT method establishes a tunnel to the server identified by the target resource.
OPTIONS
The OPTIONS method describes the communication options for the target resource.
TRACE
The TRACE method performs a message loop-back test along the path to the target
resource.
PATCH
The PATCH method applies partial modifications to a resource.
HTTP Request Methods
SAFE IDEMPOTENT
POST No No
PUT No Yes
PATCH No No
DELETE No Yes
CONNECT No No
HTTP HEADERS
• HTTP headers allow the client and the server to pass additional information with the
request or the response.
• An HTTP header consists of its name followed by a colon ':', then by its value.
• General header: Headers applying to both requests and responses but with no relation to
the data eventually transmitted in the body.
• Request header: Headers containing more information about the resource to be fetched or
about the client.
• Response header: Headers with additional information about the response, like its location
or about the server itself (name and version etc.).
• Entity header: Headers containing more information about the body of the entity, like its
content length or its MIME-type.
HTTP Headers -Content
• The Accept header lists the MIME types of media resources that the agent is willing to
process.
• Each combined with a quality factor, a parameter indicating the relative degree of
preference between the different MIME types.
• A media type also known as MIME type is a two-part identifier for file formats and format
contents transmitted on the Internet.
• Form: type/subtype
• Examples: text/plain, text/html, image/gif, image/jpeg
• Accept: image/gif, image/jpeg, */*
The Ac cep t h ea de r
HTTP Headers -Content
The Accept-Encoding header
• The Accept-Encoding header defines the acceptable content-encoding
(supported compressions).
• The value is a q-factor list (e.g.: br, gzip;q=0.8) that indicates the priority of the
encoding values.
• Compressing HTTP messages is one of the most important ways to improve the
performance of a Web site.
• Accept-Encoding:gzip, deflate
The Accept-Language header
• It is used to indicate the language preference of the user.
• Accept-Language: en-us
The User-Agent header
It identifies the browser sending the request.
HTTP Headers- Caching
• The Cache-Control general-header field is used to specify directives for caching mechanisms in both requests and
responses.
• Caching directives are unidirectional, i.e., directive in a request is not implying that the same directive is to be given in
the response.
• Standard Cache-Control directives that can be used by the client in an HTTP request.
• Cache-Control: max-age=<seconds>
• Cache-Control: no-cache
• Cache-Control: no-store
• Cache-Control: no-transform
• Standard Cache-Control directives that can be used by the server in an HTTP response.
• Cache-Control: must-revalidate
• Cache-Control: no-cache
• Cache-Control: no-store
• Cache-Control: no-transform
• Cache-Control: public
• Cache-Control: private
Ca che-Co ntrol h ea de r
HTTP Headers- Caching
• The Expires header contains the date/time after which the response is
considered stale.
• Invalid dates, like the value 0, represent a date in the past and mean that
the resource is already expired.
• The client will send the Etag value of its cached resource along in an If-None-Match
header field:
• If-None-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4“
• The server compares the client's ETag (sent with If-None-Match) with the ETag for its
current version of the resource
• If both values match, the server send back a 304 Not Modified status, without any body
• Tells the client that the cached version of the response is still good.
Etag
HTTP Headers-Caching
• The Last-Modified response HTTP header contains the date and time at
which the origin server believes the resource was last modified.
• It is used as a validator to determine if a resource received or stored is the
same.
• Less accurate than an ETag header