<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marline Khavele</title>
    <description>The latest articles on DEV Community by Marline Khavele (@marlinekhavele).</description>
    <link>https://dev.to/marlinekhavele</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1099993%2Fd134ead9-fb8b-48db-a166-de2b047b79b8.jpeg</url>
      <title>DEV Community: Marline Khavele</title>
      <link>https://dev.to/marlinekhavele</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marlinekhavele"/>
    <language>en</language>
    <item>
      <title>HTTP Request/Response Cycle</title>
      <dc:creator>Marline Khavele</dc:creator>
      <pubDate>Tue, 20 Jun 2023 16:54:03 +0000</pubDate>
      <link>https://dev.to/marlinekhavele/http-requestresponse-cycle-mb6</link>
      <guid>https://dev.to/marlinekhavele/http-requestresponse-cycle-mb6</guid>
      <description>&lt;p&gt;This is the interaction between the client which in this case is our browser and the server, where the client sends an HTTP request to the server and the server responds with an HTTP Response which is a status code.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;The main components of the HTTP Response Cycle are:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Client&lt;/strong&gt;: This is the web browser that initiates the request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server&lt;/strong&gt;: This can be a computer on the internet that is responsible for processing a request and generating a response through an HTTP protocol. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Request&lt;/strong&gt;: A client sends an HTTP request, The request contains a request method describing what action is requested and the address pointing to the resource plus information about the client, The client can also send information to the server and is packaged in the request as a payload.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Response&lt;/strong&gt;:This is the literal response to the request it contains status code information about how the response was handled, plus data requested if the information was successful. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;status codes&lt;/strong&gt;:These are Numerical codes in the range of &lt;strong&gt;100&lt;/strong&gt; to &lt;strong&gt;500&lt;/strong&gt; describing what type. of response, the server sent back to the client for example &lt;strong&gt;200 OK&lt;/strong&gt; and &lt;strong&gt;404 Not Found&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1psp6ifdz1hzco5zlz0d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1psp6ifdz1hzco5zlz0d.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the image above we see the Client initiates a HTTP request to the server, This information is sent over the network and it reaches the server. The server receives the request, process it, and generates an appropriate response. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Request header&lt;/strong&gt;&lt;br&gt;
Below is an example of an HTTP header, It can also contain a payload of data such as form submission or uploading files but for this example, we have none at the moment. All this information is passed via HTTP and the browser waits patiently for the response.&lt;br&gt;
GET &lt;a href="https://jsonplaceholder.typicode.com/photos/3" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example of a Request header.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Host:jsonplaceholder.typicode.com
User-Agent:PostmanRuntime/7.32.2
Accept:*/*
Accept-Encoding: gzip, deflate, br
Connection:keep-alive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Response header&lt;/strong&gt;&lt;br&gt;
If there is a server at the requested address specified in the request header it will return a response even if it is a 404 but in this example, the status is 200 OK. &lt;br&gt;
&lt;em&gt;Example of how a Response header.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Date: Tue, 20 Jun 2023 16:12:14 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: Express
X-Ratelimit-Limit: 1000
X-Ratelimit-Remaining: 997
X-Ratelimit-Reset: 1687192684
Vary: Origin, Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: max-age=43200
Pragma: no-cache
Expires: -1
X-Content-Type-Options: nosniff
Etag: W/"c9-5HqU0syvAt5kRuunEKDW0ay10ZE"
Via: 1.1 vegur
CF-Cache-Status: REVALIDATED
Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=pstM2R0QCYmQuSL%2B%2FT7oUR8Ksi09ZE8IFvLZTUtikjdJsUI%2F68ift8YFhT%2BNWmQvqM8XT6IZLlrKOF5GR4iFp4XI1TZvF12qSKeljPIL%2B4A%2FgZ%2Bco3vupMYkm08F2%2FYuU1iV74pQzSslunm20WA%3D"}],"group":"cf-nel","max_age":604800}
NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
Server: cloudflare
CF-RAY: 7da54acd081c19af-FRA
Content-Encoding: br
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;HTTP status Message&lt;/strong&gt;&lt;br&gt;
Here we are going to discuss some  common HTTP response status codes:&lt;br&gt;
&lt;strong&gt;1XX Information&lt;/strong&gt;: Status codes of this format are informational and rarely encountered, They inform the client of the status of the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;102 - Processing, tells the client to wait for the server to finish.
100 - Continue and the server has received the request headers and is ready for the rest of the request body.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2xx Success&lt;/strong&gt;: Codes with this format are for success messages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;200 - The request was successful.
201 - Created and it was successful.
204 - The server processed the request and returned no content. 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3XX Redirection&lt;/strong&gt;: The client is provided with a new URL to follow to get to the requested resource.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;301 - Moved permanently tells the client to use the new URI for all future requests.
302/303 - Found at this other URI means temporarily redirected.
307 - Temporary redirect
308 - Permanent redirect
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4XX Client error&lt;/strong&gt;: This signals client errors&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;400 - Bad request meaning the request is malformed or too large.
401 - Unauthorized the client lacks proper credentials to access the requested resource.
403 - Forbidden means the request is refused by the server.
404 - Not Found meaning the resource does not exist.
405 - Method not allowed is when you try to use GET on a resource that uses POST for example.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5XX Server error&lt;/strong&gt;: signals server errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;500 - Internal Server Error means something went wrong on the server.
502 - Bad gateway the server received an invalid response from where it was trying to connect.
503 - Service unavailable encountered when the server is overloaded or temporarily unavailable.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example of a request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import requests
def main():
 url = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
 response = requests.get(url)
 print(response.status_code)  # Print the response status code
 print(response.headers)  # Print the response headers
 print(response.text)
if __name__ == "__main__":
    main()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code block illustrates the HTTP response cycle. we have used a package called requests from Python to help us in getting the data that we need, In this case, we want to see the &lt;em&gt;headers&lt;/em&gt;,&lt;em&gt;status_code&lt;/em&gt;, and the &lt;em&gt;actual response&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;200
{'Content-Type': 'application/json; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Date': 'Tue, 20 Jun 2023 12:45:09 GMT', 'Server': 'Apache', 'Cache-Control': 'public, max-age=60', 'Expires': 'Tue, 20 Jun 2023 12:46:09 GMT', 'Last-Modified': 'Tue, 20 Jun 2023 12:45:09 GMT', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': '*', 'Access-Control-Allow-Headers': 'accept,origin,authorization,content-type', 'X-Frame-Options': 'SAMEORIGIN', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1', 'Strict-Transport-Security': 'max-age=31536000', 'Vary': 'Accept-Encoding', 'X-Cache': 'Hit from cloudfront', 'Via': '1.1 96d2df8d2655bf1d48c2f4e4db090ae6.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'TXL50-P1', 'X-Amz-Cf-Id': 'G8XIDr6k96Vtx5jV50Up6TEMz24o_q5hea8C-Cv4agzibaScZJ-ntQ==', 'Age': '39'}
{"type":"FeatureCollection","metadata":{"generated":1687265109000,"url":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson","title":"USGS Magnitude 2.5+ Earthquakes, Past Day","status":200,"api":"1.10.3","count":37},"features":[{"type":"Feature","properties":{"mag":3.48,"place":"122 km N of Suárez, Puerto Rico","time":1687259666390,"updated":1687263668876,"tz":null,"url":"https://earthquake.usgs.gov/earthquakes/eventpage/pr2023171003","detail":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/pr2023171003.geojson","felt":null,"cdi":null,"mmi":null,"alert":null,"status":"reviewed","tsunami":0,"sig":186,"net":"pr","code":"2023171003","ids":",pr2023171003,","sources":",pr,","types":",origin,phase-data,","nst":12,"dmin":1.2585,"rms":0.33,"gap":281,"magType":"md","type":"earthquake","title":"M 3.5 - 122 km N of Suárez, Puerto Rico"},"geometry":{"type":"Point","coordinates":[-65.7848,19.5316,47]},"id":"pr2023171003"},
{"type":"Feature","properties":{"mag":4.7,"place":"Potosi, Bolivia","time":1687251870487,"updated":1687254026040,"tz":null,"url":"https://earthquake.usgs.gov/earthquakes/eventpage/us7000k9tr","detail":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/us7000k9tr.geojson","felt":null,"cdi":null,"mmi":null,"alert":null,"status":"reviewed","tsunami":0,"sig":340,"net":"us","code":"7000k9tr","ids":",us7000k9tr,","sources":",us,","types":",origin,phase-data,","nst":24,"dmin":0.711,"rms":0.62,"gap":84,"magType":"mb","type":"earthquake","title":"M 4.7 - Potosi, Bolivia"},"geometry":{"type":"Point","coordinates":[-67.7983,-22.331,176.354]},"id":"us7000k9tr"},

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Error handling in  HTTP Response Cycle, This can be a &lt;strong&gt;404&lt;/strong&gt;,&lt;strong&gt;401&lt;/strong&gt;  indicating a resource wasn’t found or authorized. With such an error message we can extract relevant information about the error and take the appropriate steps and actions. &lt;br&gt;
HTTP response cycle gives us a mechanism of effective error handling, The allows the client and the server to communicate and handle errors well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "error": {
    "code": 404,
    "message": "The requested resource was not found."
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>python</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
