1

I would like to get response HTTP status_code for monitor services using flask python.

My code:

from flask import Flask, request, url_for, redirect, render_template, flash, Response, abort

import requests

res = requests.get('https://192.168.1.100:8000/token?api=API',verify=False)
app = Flask(__name__)
print(res.url) # your.domain/test/3/3

print(res.status_code) # 200

@app.route('/services')
def check_services():
   if request.method == "GET" or request.method == "POST": 
        if res.status_code == '201':
                result =  'Sucess HTTP.Status_code 201 - Service TOKEN is working fine [OK]'
               return render_template('services.html'), 201
#              result = '401 -- Service TOKEN is working fine - parameter is not correct'

        else:
#                abort(401)  
               return render_template('services401.html'),401  

but my code didnot work right.

My expected result, it will be returned by seperate HTTP status code: 201 or 401 or 500 whatever which I define in if condition.

Any help apprecated !!!

1 Answer 1

2

The status codes are int, not str. So, check with 201, not '201'. Your code should be:

if request.method == "GET" or request.method == "POST": 
    if res.status_code == 201:
        # do what you want here

See below for details.

>>> import requests
>>> r = requests.get('https://httpbin.org/get')
>>> r.status_code
200
>>> type(r.status_code)
<class 'int'>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.