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
    • 733
    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:-

    # this command is to install requests library for python 3.6
    
    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.

            

    import sys
    import requests
    import urllib
    import json
    
    
    url = 'PASTE HERE YOUR URL'
    
    newConditions = {'email': 'test@test.com', 'password': '123123', 'device_token': '123456','device_type':'iOS'} # SET YOUR PARAMETERS FOR REQUEST
    params = json.dumps(newConditions).encode('utf8')
    req = urllib.request.Request(url, data=params,
                                 headers={'content-type': 'application/json'})
    response = urllib.request.urlopen(req)
    
    raw_data = response.read()
    encoding = response.info().get_content_charset('utf8')  # JSON default
    data = json.loads(raw_data.decode(encoding))
    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.
    
    #!/usr/bin/python3
    
    import sys
    import requests
    import urllib
    import json
    
    url = 'PASTE YOUR GET URL HERE'
    
    req = urllib.request.Request(url,
                                 headers={'content-type': 'application/json'})
    response = urllib.request.urlopen(req)
    
    raw_data = response.read()
    encoding = response.info().get_content_charset('utf8')
    data = json.loads(raw_data.decode(encoding))
    print(data)

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: