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.