Requests API


  • Requests is a library for performing various HTTP operations from Python Code.
  • Requests has got several accolades mainly for the simplicity of the interface it offers to a Python Programmer.
  • Requests abstracts developers from writing a lot of network code and helps getting things done in a rapid yet elegant way.


Installing Requests:

Requests library can be installed using pip command as given here:

pip install requests


Request a HTTP server for a resource:

  • The requests module offers the main interface, using which any required HTTP operation can be performed.
  • The methods of the requests module return a response object.
  • The get method of the requests module sends a HTTP GET command to the HTTP server specified and returns the HTTP response as a response object.


A Python example using requests to fetch the homepage from

  • After making a HTTP GET request to the web server of, the results or the HTTP response is returned through a response object.
  • The response object is checked for the status of the HTTP request sent.
  • From the response object, the values of the HTTP header fields and the HTML contents are printed.

import requests


# Connect to the web server of and get the home page

url2Fetch = ""

print("Sending HTTP GET to %s"%url2Fetch)

response = requests.get(url2Fetch)


# Print the http response status code

print("HTTP response code: %d"%response.status_code)


# Print the HTTP headers

print("HTTP Header: %s"%response.headers)


# Print the HTML text

print("HTTP Header: %s"%response.text)



Sending HTTP GET to

HTTP response code: 200

HTTP Header: {'Content-Encoding': 'gzip', 'Accept-Ranges': 'bytes', 'Cache-Control': 'max-age=604800', 'Content-Type': 'text/html; charset=UTF-8', 'Date': 'Tue, 29 Jan 2019 16:51:05 GMT', 'Etag': '"1541025663+gzip"', 'Expires': 'Tue, 05 Feb 2019 16:51:05 GMT', 'Last-Modified': 'Fri, 09 Aug 2013 23:54:35 GMT', 'Server': 'ECS (dca/24C0)', 'Vary': 'Accept-Encoding', 'X-Cache': 'HIT', 'Content-Length': '606'}

HTTP Header: <!doctype html>



    <title>Example Domain</title>


    <meta charset="utf-8" />

    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <style type="text/css">

    body {

        background-color: #f0f0f2;

        margin: 0;

        padding: 0;

        font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;



    div {

        width: 600px;

        margin: 5em auto;

        padding: 50px;

        background-color: #fff;

        border-radius: 1em;


    a:link, a:visited {

        color: #38488f;

        text-decoration: none;


    @media (max-width: 700px) {

        body {

            background-color: #fff;


        div {

            width: auto;

            margin: 0 auto;

            border-radius: 0;

            padding: 1em;








    <h1>Example Domain</h1>

    <p>This domain is established to be used for illustrative examples in documents. You may use this

    domain in examples without prior coordination or asking for permission.</p>

    <p><a href="">More information...</a></p>




Copyright 2023 ©