<?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: Mobey-eth</title>
    <description>The latest articles on DEV Community by Mobey-eth (@mobeyeth).</description>
    <link>https://dev.to/mobeyeth</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%2F943566%2F182a91d5-5334-41cc-959f-cf4fa5e559ae.png</url>
      <title>DEV Community: Mobey-eth</title>
      <link>https://dev.to/mobeyeth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mobeyeth"/>
    <language>en</language>
    <item>
      <title>How to send an HTTP request with Python</title>
      <dc:creator>Mobey-eth</dc:creator>
      <pubDate>Sun, 05 Nov 2023 07:51:35 +0000</pubDate>
      <link>https://dev.to/mobeyeth/how-to-send-an-http-request-with-python-41bh</link>
      <guid>https://dev.to/mobeyeth/how-to-send-an-http-request-with-python-41bh</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;HTTP (Hypertext Transfer Protocol) is a fundamental protocol for data communication between clients and servers on the Internet. It enables the exchange of information between a client (e.g., a web browser) and a server (e.g., a web application). &lt;/p&gt;

&lt;h2&gt;
  
  
  What is an HTTP Request?
&lt;/h2&gt;

&lt;p&gt;An HTTP request is a message that a client sends to a server over the Hypertext Transfer Protocol (HTTP). It is used to transfer all sorts of data, including HTML pages, images, videos, and JSON objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTP requests are made up of four parts&lt;/strong&gt;:&lt;br&gt;
The request method: This specifies the type of request being made. The most common request methods are GET, POST, PUT, and DELETE.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The request URL&lt;/strong&gt;: This is the address of the resource that the client is requesting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The request headers&lt;/strong&gt;: This is a set of key-value pairs that contain additional information about the request. For example, the Content-Type header specifies the type of data that is being sent in the request body.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The request body&lt;/strong&gt;: This is the optional body of the request, which can contain data such as form data, JSON objects, or XML documents.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Choosing the Right Library
&lt;/h2&gt;

&lt;p&gt;Python offers various libraries for making HTTP requests, including httplib, urllib, and requests. In this article, we will use the "requests" library for its simplicity.&lt;/p&gt;
&lt;h3&gt;
  
  
  Installing the Requests Library
&lt;/h3&gt;

&lt;p&gt;Before we begin, ensure you have the "requests" library installed. If not, you can easily install it running the following command on your terminal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install requests&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Making a GET Request with Python:&lt;br&gt;
Let's put our knowledge into practice by making a GET request to a Weather API to retrieve real-time  weather information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;

&lt;span class="c1"&gt;# Define an API endpoint URL
&lt;/span&gt;&lt;span class="n"&gt;URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"http://api.weatherapi.com/v1/current.json"&lt;/span&gt;

&lt;span class="c1"&gt;# Specify the location and API key
&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;'q'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"London"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# Location
&lt;/span&gt;    &lt;span class="s"&gt;'key'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"YOUR_API_kEY"&lt;/span&gt;  &lt;span class="c1"&gt;# Your API key
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Send a GET request to the API endpoint with the specified parameters in JSON format
&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Check if the request was successful (status code 200)
&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Extract and parse the JSON response
&lt;/span&gt;    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Extract specific information from the JSON response
&lt;/span&gt;    &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'location'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'country'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'current'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;temp_celsius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'temp_c'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'condition'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s"&gt;'text'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;windspeed_mph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'wind_mph'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="c1"&gt;# Print the extracted information
&lt;/span&gt;    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"City: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Country: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Temperature (Celsius): &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;temp_celsius&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;°C"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Condition: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Windspeed (MPH): &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;windspeed_mph&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; mph"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Print an error message if the request failed
&lt;/span&gt;    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Request failed with status code &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x4sBKxIg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/340evtm21ik6l2kntie3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x4sBKxIg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/340evtm21ik6l2kntie3.png" alt="Image description" width="403" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Explanation of the Code&lt;/p&gt;

&lt;h3&gt;
  
  
  Imports:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import requests
import json
import time

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

&lt;/div&gt;



&lt;p&gt;We start by importing the required libraries for this project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining the API Endpoint URL and Parameters:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;URL = "http://api.weatherapi.com/v1/current.json"
params = {
    'q': "London",  # Location
    'key': "YOUR_API_kEY"  # Your API key
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We define the URL and specify the parameters. In this case, we set the address parameter to the location we want to retrieve weather information about.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sending a GET Request:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;response = requests.get(URL, params=params&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;We use the requests.get method to send a GET request to the API endpoint with the specified parameters.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling the Response:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;if response.status_code == 200:&lt;br&gt;
    data = response.json()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We check if the request was successful (status code 200), and if it is, we parse the response as JSON using the response.json() method.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extracting Specific Information from JSON Response :
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;location = data['location']
city = location['name']
country = location['country']
temp_celsius = data['current']['temp_c']
condition = data['current']['condition']['text']
windspeed_mph = data['current']['wind_mph']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We extract specific information from the JSON response, such as the city, country, temperature in Celsius, condition, and windspeed in MPH.&lt;/p&gt;

&lt;h3&gt;
  
  
  Printing the Extracted Information:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(f"City: {city}")
print(f"Country: {country}")
print(f"Temperature (Celsius): {temp_celsius}°C")
print(f"Condition: {condition}")
print(f"Windspeed (MPH): {windspeed_mph} mph")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we print the extracted information to the console in an easy-to-understand format.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we've explored the world of HTTP requests, focusing on the GET method. We've learned how to send HTTP requests in Python using the "requests" library.&lt;/p&gt;

&lt;p&gt;The Python Requests library makes it easy to send HTTP requests in Python. It provides a simple and easy-to-use interface for making all types of HTTP requests.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>python</category>
      <category>requests</category>
    </item>
  </channel>
</rss>
