My QA Projects

QA Projects I was involded.

View on GitHub

What is the Requests Module?

Requests Installation

pip3 install requests

Uniform Resource Locator

URL Parts

Fetching Internet Resources

user » sending a request » server server » sending a response which contains the resource » client

import request module » prepare request >. send request » get response » process response

Import the Requests Module

import requests

Preparing the Request

Sending the Request

Get Method

import requests
requests.get(url, parameters={})

reteieve the url resource and return a response object parameters are mebedded and encouded in the URL.

Getting the Response

import request module » prepare request » send request » get response

Processing the Response

if the GET or the POST call receive a response check the status by using response.ok is True:

import request module » prepare request >. send request » get response » process response

Response Properties

Common HTTP Status Codes

     
200 Successful Response nothing more to do
400 Bad Request 400-499 indicate error on client side
401 Unauthorized  
404 Reource Not Found might also be a typo in url
500 Internal Server Error there is nothing we can do about

Text Contents

Binary Contents

Response Object Properties

response.content

response.text

Requests Example

import request

url = 'https://www.python.org'
response = requests.get(url)
with open ('index.html', 'w') as html_file:
    if response.ok:
        html_file.write(response.text)
        print('finished printing')
import request

url = 'https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/440px-Wikipedia-logo-v2.svg.png'
response = requests.get(url)
with open ('wiki-logo.png', 'wb') as png_file:
    # set the context manager to download a binary file
    if response.ok:
        png_file.write(response.content)
        print('finished printing')