12 – HTTP Requests in Python

HTTP REQUESTS:

HTTP stands for Hypertext Markup Language.  HTTP works as a request-response protocol in server-client communication. The client requests the server for a webpage or other format of data, in response the server sends the required data in HTML format or other.

 The server hosted on any corner of this world can be accessed using the World Wide Web(WWW). If the server is hosted on a local network or a private network then it is available only on that network.

The server can be requested using the mainly used two methods namely,

  • GET 
  • POST

GET METHOD:

GET requests are mostly used to requests data from the source.  The basic syntax is given below.

https://www.example.com/path_to_page/field1=value1&field2=value2...

Our daily partner google search is an example of GET method. For example, when I search for mybtechprojects in google the query URL will be as shown below.

  • GET method is not secure because the data is sent to the server is revealed in the URL. 
  • Passwords or other sensitive pieces of information cannot be sent using the GET method.
  • Only limited amount of data can be sent using the GET method.
  • GET requests can be cached and so the data sent is visible in browser history.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import requests
import json
r=requests.get("https://mybtechprojects.000webhostapp.com/testdata.json")
 
#To print the response code "200 is for success"
print "Code = ",r
 
#To print the text
print "Text = ",r.text
 
j=json.loads(r.text)
#To print json Data
print j["website_name"]
print j["no_of_posts"]

OUTPUT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>> 
Code =  <Response [200]>
Text =  {
 
    "website_name":"MYBTECHPROJECTS",
 
    "website_url":"www.mybtechprojects.tech",
 
    "no_of_posts":30,
 
    "Categories": ["Arduino","Esp8266","python","NodeMcu","Raspberry PI"]
 
}
MYBTECHPROJECTS
30
>>>

I have uploaded the php files in my server https://mybtechprojects.000webhostapp.com for test purpose. If you want to create your own server, visit this post.

get.php

1
2
3
4
5
<?php
   $name= $_GET["name"];
   $age=$_GET["age"];
   echo "User Name = ".$name."  Age = ".$age;
 ?>

PYTHON CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import requests
import json
URL="https://mybtechprojects.000webhostapp.com/get.php"
 
Payload= {'name':"Mybtechprojects",'age':"21"}
 
# Sending get request 
r = requests.get(url = URL, params = Payload)
 
#Response Code
print r
 
#Output as text
 
print r.text

OUTPUT

1
2
3
4
>>> 
<Response [200]>
User Name = Mybtechprojects  Age = 21
>>>

POST METHOD:

In POST method the data’s are enclosed with the response body. So the parameters or not visible in URL. Sensitive pieces of information like usernames and passwords are sent using POST method. 

  • Data is not limited in POST method.
  • Data is secure since the data is not visible in URL.
  • Data is not cached and bookmarked

post.php

1
2
3
4
5
<?php
   $name= $_POST["name"];
   $age=$_POST["age"];
   echo "User Name = ".$name."  Age = ".$age;
 ?>

PYTHON CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import requests
import json
URL="https://mybtechprojects.000webhostapp.com/post.php"
 
Payload= {'name':"Mybtechprojects",'age':"21"}
 
# Sending get request 
r = requests.post(url = URL, data = Payload)
 
#Response Code
print r
 
#Output as text
 
print r.text

OUTPUT

1
2
3
4
>>> 
<Response [200]>
User Name = Mybtechprojects  Age = 21
>>>
THANK YOU

 

SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.