requests.Session. - python examples

Here are the examples of the python api requests.Session. taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

7 Examples 7

3 View Complete Implementation : exploit_1.py
Copyright MIT License
Author : HackerDom
def add_product(token_header, manufacturer):
    product = {
        "name": gen_r_string(),
        "protein": randint(1, 10),
        "fat": randint(1, 10),
        "carbohydrate": randint(1, 10),
        "calories": randint(1, 10),
        "manufacturer": manufacturer
    }
    res = Session().post(f"{URL}/api/Products", json=product, headers=token_header, allow_redirects=False)
    if res.status_code != 201:
        print('[-] Can not add product')
        exit()
    print('[+] Product added')
    return res.json()

3 View Complete Implementation : exploit_1.py
Copyright MIT License
Author : HackerDom
def create_dish(token_header, ingredients):
    payload = {
        "recipe": gen_r_string(),
        "ingredients": ingredients,
        "name": gen_r_string(),
        "description": gen_r_string(),
        "portionWeight": randint(100, 200)
    }
    res = Session().post(f"{URL}/api/Dishes", json=payload, headers=token_header, allow_redirects=False)
    if res.status_code != 201:
        print('[-] Can\'t create dish')
        exit()
    print('[+] Dish created')
    return res.json()

3 View Complete Implementation : exploit_api.py
Copyright MIT License
Author : HackerDom
def add_product(token_header, manufacturer, url):
    product = {
        "name": gen_rand_string(),
        "protein": randint(1, 10),
        "fat": randint(1, 10),
        "carbohydrate": randint(1, 10),
        "calories": randint(1, 10),
        "manufacturer": manufacturer
    }
    res = Session().post(f"{url}/api/Products", json=product, headers=token_header, allow_redirects=False)
    if res.status_code != 201:
        print('[-] Can not add product')
        exit()
    print('[+] Product added')
    return res.json()

3 View Complete Implementation : exploit_api.py
Copyright MIT License
Author : HackerDom
def create_dish(token_header, ingredients, url):
    payload = {
        "recipe": gen_rand_string(),
        "ingredients": ingredients,
        "name": gen_rand_string(),
        "description": gen_rand_string(),
        "portionWeight": randint(100, 200)
    }
    res = Session().post(f"{url}/api/Dishes", json=payload, headers=token_header, allow_redirects=False)
    if res.status_code != 201:
        print('[-] Can not create dish')
        exit()
    print('[+] Dish created')
    return res.json()

3 View Complete Implementation : exploit_api.py
Copyright MIT License
Author : HackerDom
def get_dish(token_header, dish_id, url):
    res = Session().get(f"{url}/api/Dishes/{dish_id}", headers=token_header, allow_redirects=False)
    if res.status_code != 200:
        print('[-] Dish not found')
        return None
    return res.json()

3 View Complete Implementation : exploit_api.py
Copyright MIT License
Author : HackerDom
def get_dishes(token_header, url, skip=0, take=100):
    res = Session().get(f"{url}/api/Dishes?take={take}&skip={skip}", headers=token_header, allow_redirects=False)
    if res.status_code != 200:
        print('[-] Dishes not found')
        return None
    return res.json()

3 View Complete Implementation : exploit_api.py
Copyright MIT License
Author : HackerDom
def import_products_by_xml(token_header, products_xml, url):
    res = Session().post(f"{url}/api/products/import",
                         headers=token_header,
                         files={'xml': ('"xml"', products_xml)},
                         allow_redirects=False)
    if res.status_code != 200:
        print('[-] Can not import xml')
        return

    print('[+] Import xml successfully')
    return res.json()