Denisa Buzan
Denisa Buzan

Reputation: 41

Create a middleware which listens to localhost:3332 and prints the endpoints called

I am trying to create a system in java which listens to localhost:3332 and prints the endpoints. I have a application which runs on that port where I can apply various actions to a table.

I have tried to run this script :

url=url = 'http://127.0.0.1:3332/'
while True:
    with requests.Session() as session:
    response = requests.get(url)
    if response.status_code == 200:
        print("Succesful connection with API.")
        // here I should print the endpoints

Unfortunately, it doesn't work. Any suggestion is more than welcome

Upvotes: -1

Views: 36

Answers (1)

Juan Melnechuk
Juan Melnechuk

Reputation: 458

The script doesn't work because the "with requests.Session() as session" command is missing parentheses, which are necessary for command execution. Correcting this will fix the issue.

Also, it's not clear what you mean by printing the endpoints. Depending on the application, you may need to modify the script in order to make an API call that will return the endpoints in this way:

url = "http://127.0.0.1:3333/endpoints"

with requests.Session() as session:
    response = requests.get(url)
    if response.status_code == 200:
        print("Succesful connection with API.")
        // here I should print the endpoints - assuming the API call gives json data
        data = response.json()
        if data:
            for endpoint in data:
                print("Endpoint:", endpoint)

Hope this helps.

Upvotes: 0

Related Questions