Requests = Blank Output

Executing a simple API call using requests. Receiving NO output and return code is success. Could use some help to identify my deficiency, please see below:

SCRIPT:

# <snip>
# Install the Python Requests library:
# `pip install requests`

import requests


def send_request():

    try:
        response = requests.get(
            url="https://<REDACTED>",
            headers={
                "Accept": "application/json",
                "Content-Type": "application/json",
                "X-ApiKeys": "<REDACTED>",
                "Cookie": "nginx-cloud-site-id=mssp-us-1",
            },
        )
        print('Response HTTP Status Code: {status_code}'.format(
            status_code=response.status_code))
        print('Response HTTP Response Body: {content}'.format(
            content=response.content))
    except requests.exceptions.RequestException:
        print('HTTP Request failed')
# </snip>

OUTPUT:

TJONES-MB:Desktop user$ python3 ./APITestScript.py

TJONES-MB:Desktop user$ echo $?

0

The import statement and def header should be included in the block by moving the first backtick line up, Edit by clicking the pencil icon.

The output is correct since you did not call send_request(). Add this to the bottom of your code.

I think you need to call the send_request() function

import request


def send_request():
    try:
        response = requests.get(
            url="https://<REDACTED>",
            headers={
                "Accept": "application/json",
                "Content-Type": "application/json",
                "X-ApiKeys": "<REDACTED>",
                "Cookie": "nginx-cloud-site-id=mssp-us-1",
            },
        )
        print(
            "Response HTTP Status Code: {status_code}".format(
                status_code=response.status_code
            )
        )
        print("Response HTTP Response Body: {content}".format(content=response.content))
    except requests.exceptions.RequestException:
        print("HTTP Request failed")


if __name__ == "__main__":
    send_request()

You’re defining a function, but not calling it. Try adding this at the end:

send_request()

Or just backtab all the code so it isn’t in the function any more; that’s also valid.