"Typeerror int is not collable" from http requests.post()

Question:

I know this question has come up a lot, but I couldn’t find any suitable solution for my problem.
from the library requests I try to use something like this:

response = requests.post(url, headers, data, proxies) 

if response.status_code >= 300:
   logging.ERROR(f"MY ERROR MESSAGE")
elif response.status_code < 300:
   logging.INFO(f"MY INFO MESSAGE")
else:
   logging.ERROR(f"MY ERROR MESSAGE 2")

This always comes back with the following error message:

Traceback (most recent call last):
  File "/var/task/my_little_file.py", line 36, in my_function
    logging.INFO(f"MY INFO MESSAGE") 

If I print out the response, the type of the response, the status code and also the type of the status code, I’ll receive the following output:

print("this is the response {}".format(response))
print(type(response))
print("status code: {}".format(response.status_code))
print("type status code: {}".format(type(response.status_code)))


>>> this is the response <Response [201]> 
>>> <class 'requests.models.Response'> 
>>> status code: 201
>>> type status code: <class 'int'> 

So the status_code is an integer, but somehow the if-loop does not interpretate it correctly..
Any help is appreciated 🙂

I run this on Python 3.9

Asked By: Daniel_T

||

Answers:

Problem is not with requests, but how do you use logging. All-uppercase things in logging are constants, if you meant to use function use all-lowercase, that is replace

logging.ERROR(f"MY ERROR MESSAGE")

using

logging.error(f"MY ERROR MESSAGE")

and

logging.INFO(f"MY INFO MESSAGE")

using

logging.info(f"MY INFO MESSAGE")

As side note observe that else-branch will never be reached, as status code is either greater-equal 300 or less than 300, tertium non datur.

Answered By: Daweo