Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • HTTP request using python

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 737
    Comment on it

    Hi All,

    HTTP request  is a two way process i..e client - server communication .Three basic features that make HTTP a powerful protocol and these are:

    • HTTP is connection less
    • HTTP is media independent
    • HTTP is stateless:

    There are many type of request like :-

    • GET
    • POST
    • DELETE
    • PUT

    In this tutorial , we will use python to implement these request . Default libraries are used in this tutorial if you got an error of "No module name called 'requests' found " then use this command to install request library:-

    1. # this command is to install requests library for python 3.6
    2.  
    3. sudo pip3.6 install requests
    • POST Request :- A POST request is used to send data to the server as this method provide more security while transferring information, we can fetch user private data using this method and make changes in server data.To implement this request we use requests , urllib , json library.

            

    1. import sys
    2. import requests
    3. import urllib
    4. import json
    5.  
    6.  
    7. url = 'PASTE HERE YOUR URL'
    8.  
    9. newConditions = {'email': 'test@test.com', 'password': '123123', 'device_token': '123456','device_type':'iOS'} # SET YOUR PARAMETERS FOR REQUEST
    10. params = json.dumps(newConditions).encode('utf8')
    11. req = urllib.request.Request(url, data=params,
    12. headers={'content-type': 'application/json'})
    13. response = urllib.request.urlopen(req)
    14.  
    15. raw_data = response.read()
    16. encoding = response.info().get_content_charset('utf8') # JSON default
    17. data = json.loads(raw_data.decode(encoding))
    18. print(data)

     

    • GET Request :-The get method is used to retrieve data from server as this method does not provide so much security so we use this request to get general data not user private information or data. In this request we may or may not be send parameters as it will be appended to URI.
    1. #!/usr/bin/python3
    2.  
    3. import sys
    4. import requests
    5. import urllib
    6. import json
    7.  
    8. url = 'PASTE YOUR GET URL HERE'
    9.  
    10. req = urllib.request.Request(url,
    11. headers={'content-type': 'application/json'})
    12. response = urllib.request.urlopen(req)
    13.  
    14. raw_data = response.read()
    15. encoding = response.info().get_content_charset('utf8')
    16. data = json.loads(raw_data.decode(encoding))
    17. print(data)

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: