introduction Flashcards
make a simple HTTP request using the curl command and analyze the output.
curl -iX GET https://reqres.in/api/users/1
The server response has HTTP headers and a message body.
Response headers as key/value pairs
The first line is the HTTP status code
One of the keys is a content-type and has a corresponding value
One of the keys is a set-cookie and has a corresponding value
Cookies are
a unique identification number that help us keep track of our activity on different web pages.
When you first visit the website, the server assigns the unique identification number to the client. After the cookie is set, the client will always request the server using the cooki
The second part of the HTTP response is
the message body, which is in the JSON format.
The curl command is a command-line tool used to transfer data to or from a server,
with the help of any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP or FILE)
HTTP resources are identified and located on the network by
Uniform Resource Locators (URLs), using the Uniform Resource Identifiers (URIs) schemes HTTP and HTTPS.
https://www.educative.io/explore
https – protocol ( Secure HTTP )
www – subdomain
educative.io – domain name
/explore – service end point
HTTP methods are designed to enable communications between clients and servers. We will discuss the most commonly used HTTP methods in the next few lessons.
GET
POST
PUT
DELETE
The GET method is used by the client to
fetch the data from the server.
By default, curl requests the URL using the GET method. To request the specific URL using the POST, DELETE or PUT methods,
we have to use the -X right after the curl command and specify the name of a particular method.
The output is a JSON response returned from the server for the requested specified resource.
A JSON format will have a key/value pair that represents the data enclosed by curly braces.
Using the -X switch will just show us the
HTML content in the form of the message body.
The -iX switch will show us a
response header as well as HTML content in the form of the message body
the -I switch will just show us
the response header.
The HTTP POST method sends data to the server to
create a new resource.
curl -iX POST -H “Content-Type: application/json” -d ‘{ “email”: “eve.holt@reqres.in”, “password”: “pistol” }’ https://reqres.in/api/register
-iX is for HTTP Method
-H is short for –header
-d for –data
The HTTP PUT method will
update an existing resource or create a new resource on the server, (depending on the request payload.)
curl -X PUT -d ‘{“title”:”This is an updated post”}’ https://jsonplaceholder.typicode.com/posts/1
-X is for HTTP Method
-d for –data
The HTTP DELETE method
deletes the specified resource on the server.
The response of DELETE request is empty { } JSON and the HTTP response status code is 204 OK. which means
that the resource is deleted
HTTP requests are
messages sent by the client to initiate an action on a resource identified by a given URL over the network
The components of an HTTP request are as below
The method to be applied to the resource – an HTTP method (like GET, PUT, POST HEAD or OPTIONS)
The resource identifier – resource identified by a given request URL.
Headers (optional) – headers are the information sent to the server in the form of key/value pairs, which contains the details of what the browser wants and will accept from the server.
Params (optional) – used for sending additional data to the server. For example, a query parameter.
Message body (optional) – These are additional information required by the server to process current requests properly. For example, a file type of JSON or XML sent in the case of a POST or PUT method request.
After receiving and processing an HTTP request message, a server responds with
an HTTP response message
The response returned from the server may contains
A status-line ( contains HTTP version and HTTP Status code ) – HTTP/2 200
Headers – these are general, response, or entity fields.
Response message body – a message in the form of a JSON, which returns the data associated with the post (id =1 )
In a server’s HTTP response, status codes indicate
whether a specific HTTP request has been successfully completed or an error has occurred.
The HTTP response status codes are divided into 5 categories
1xx: informational
2xx: success
3xx: redirection
4xx: client error
5xx:server error
200 OK
The request has succeeded.
201 Created
The request has been fulfilled and a new resource has been created.
202 Accepted
The request has been accepted for processing, but the processing has not been completed.
204 No Content
The request has succeeded and there is no content for the response message body.
301 Moved Permanently
The target resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs.
302 Found
The target resource resides temporarily under a different URI.
400 Bad Request
The server cannot process the request due to a bad/invalid request from a client (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
401 Unauthorized
The request lacks valid authentication credentials for the target resource.
403 Forbidden
There is an authorization error on the server.
404 Not Found
The origin server did not find a current representation for the target resource.
500 Internal Server Error
The server encountered an unexpected condition.
502 Bad Gateway
The server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.
503 Service Unavailable
The server is currently unavailable to handle the request.
504 Gateway Timeout
The server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.
What are HTTP headers?
basically a key-value, where its key is a case-insensitive string and value is a single or array of string.
to pass some additional information through a request or a response.
Types of HTTP headers
General headers
Request headers
Response headers
Entity headers
General headers
used for both request and response and there is no relation to the message data
Request headers
client headers, sent during the HTTP request and they contain information about the resource to be fetched.
Response headers
server headers and are part of the HTTP response. They give more details about the server resource context.
Entity headers
these headers give meta-information about the content of the body of the message like MIME type, Content-length, etc.
-I options in the curl command.
show only response headers from the web server in the form of key/value pairs. There will be no response message body shown
What are HTTP parameters?#
When a client uses the HTTP GET method to request a certain resource from the web server, the client may also send certain parameters through the requested URL in the form of a query parameter, path parameter, or a form parameter.
Query parameter#
The query parameter is a name-value pair and is the part of a requested GET URL.
curl -X GET https://reqres.in/api/users?page=2
path parameter
part of a requested GET URL path
curl -X GET https://reqres.in/api/users/2
Form parameter#
In an HTTP POST request a certain piece of information is sent as a form parameter in the form of a name-value pair.
The client sends the form parameters in the requested body in the format that the Content-Type header field specifies.
curl -iX POST –form “note=test” –form upfile=@sample.txt “content-type: multipart/form-data” https://cgi-lib.berkeley.edu/ex/fup.cgi
The HTTP response contains the headers and the file content in the HTML format.
REST, or Representational State Transfer, is an
architectural design that defines a set of rules for creating web services that interact between systems.
Web services that follow REST architecture are termed
‘RESTful web services’
RESTful system consists of two major components:
A server that hosts the resources
A client that connects to the server to fetch the resources
REST uses HTTP or HTTPS to exchange data between client and server using
HTTP methods – GET, POST, UPDATE, DELETE, HEAD, PATCH, etc.
benefit from the loose coupling between the server and the client when REST is followed
Simplicity
Light-weight
Architecture is similar to Web
Scalability
Simplicity benefit for REST architecture
REST web services are easy and simple to develop compared to SOAP web services
Light-weight benefit for REST architecture
REST advocates simple communication with the server over HTTP that supports plain XML, JSON formats in comparison to SOAP which supports only XML
“Architecture is similar to Web” benefit for REST architecture
developers who understand the web can easily understand and develop RESTful web services
Scalability benefit for REST architecture
conversational state that allows us to easily add multiple instances of the components or application behind load balancers
A RESTful service needs to comply with
the following 6 guiding constraints:
Client–Server
Stateless
Cacheable
Uniform Interface
Layered System
Code on-demand
Client–Server constraints for a RESTful service
separation of concerns
By following this principle, we can improve the:
portability of the user interface across platforms
scalability by simplifying the server component
Stateless constraints for a RESTful service
each request from the client must be sent to the server with all the necessary information to understand the request
Cacheable constraints for a RESTful service
cache constraints require that the data within a response to a request be implicitly or explicitly denoted as cacheable or non-cacheable
If a response is cacheable, then a client cache is given the right to reuse that response data for later, equivalent requests. This allows us to minimize the network calls made to the server.
Uniform Interface constraints for a RESTful service
It simplifies and decouples the architecture of the system, allows us to independently scale the components, and improve the interactions between components and other systems.
Layered System constraints for a RESTful service
allows an architecture to be divided into a number of hierarchical layers or tiers by constraining each of the layer’s behavior such that each layer cannot access beyond the immediate layer with which they are interacting with.
Code on-demand constraints for a RESTful service
REST allows client functionality to be extended by downloading executable scripts that can be executed on client-side
The four architectural constraints that RESTful system should follow in order to have a uniform interface are:
resource identification
manipulation of resources through representations
self-descriptive messages
hypermedia as the engine of application state (HATEOS)