Python Forum
Python requests module doesn not return status_code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python requests module doesn not return status_code
#1
I try to create definiton, which returns some data when status_code is 200, but when status_code is 404 then returns status_code. If there are others errors then def returns error message.

def Read_POD(url, auth, data, dataframe=False):
    try:
        url = url + "api/v1/Catalog/NestScrollApi"
        req = requests.post(url, auth = auth, verify=False, json=data)
        if req.status_code == 404:
            return {"response": req.status_code}
        else:
            fin = req.json()
            xp = pd.DataFrame(fin)  
            return {"data": xp, "response": req.status_code}   
    except requests.exceptions.RequestException as e:
       return "Error: {}".format(e) 
What is wrong? ... because when I am waiting that returns status_code 404 I get error:

Quote: raise ValueError('If using all scalar values, you must pass'

ValueError: If using all scalar values, you must pass an index
Reply
#2
remove the try/except and post the full traceback you get
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(May-21-2019, 07:15 AM)buran Wrote: remove the try/except and post the full traceback you get

Code after corrections:
def Read_POD(url, auth, data, dataframe=False):

    url = url + "api/v1/Catalog/NestScrollApi"
    req = requests.post(url, auth = auth, verify=False, json=data)
    if req.status_code == 404:
        return {"response": req.status_code}
    else:
        fin = req.json()
        xp = pd.DataFrame(fin)   
        return {"data": xp, "response": req.status_code}
Error:
Read_POD(tss_url, auth = auth, data = data) Traceback (most recent call last): File "<ipython-input-559-846437feeaef>", line 1, in <module> Read_POD(tss_url, auth = auth, data = data) File "<ipython-input-546-fa20f27d73e1>", line 9, in Read_POD xp = pd.DataFrame(fin) File "C:\Users\user123\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 392, in __init__ mgr = init_dict(data, index, columns, dtype=dtype) File "C:\Users\user123\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\construction.py", line 212, in init_dict return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype) File "C:\Users\user123\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\construction.py", line 51, in arrays_to_mgr index = extract_index(arrays) File "C:\Users\user123\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\construction.py", line 308, in extract_index raise ValueError('If using all scalar values, you must pass' ValueError: If using all scalar values, you must pass an index
Reply
#4
as you can see from the error, it's not problem with the requests/response code
It's a problem with pandas and creating the dataframe. Check what fin is.

Note I will move the thraed back to general coding section of the forum as it is not web related issue
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thank you. But why is problem with "fin"?
Because if req.status_code == 404 then should return {"response": req.status_code} so command with "fin" should not be executed
Or am I wrong?

And when I give right parameters into Read_POD it returned me good result without errors. Problem is only when I give wrong "data" parameter into Read_POD.
Reply
#6
(May-21-2019, 08:00 AM)palo173 Wrote: Because if req.status_code == 404 then should return {"response": req.status_code} so command with "fin" should not be executed
look at the traceback, the error is clear - the last line from your script mentioned in the traceback is line 9
Error:
File "<ipython-input-546-fa20f27d73e1>", line 9, in Read_POD xp = pd.DataFrame(fin)
after that traceback continues within pandas
So it's clear you don't get status code 404 as you expect. I guess that you get status code 200 and json that says there is problem with the data you pass. I cannot check obviously, but you can do

def Read_POD(url, auth, data, dataframe=False):
 
    url = url + "api/v1/Catalog/NestScrollApi"
    req = requests.post(url, auth = auth, verify=False, json=data)
    print(req.status_code) # print status code
    print(req.json()) # print the json response
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ModuleNotFoundError: No module named 'requests' Serg 18 2,170 Oct-29-2023, 11:33 PM
Last Post: Serg
  python requests library .JSON() error mHosseinDS86 6 3,251 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Python requests oauth2 access token herobpv 6 3,780 Sep-27-2021, 06:54 PM
Last Post: herobpv
  Help with requests module 0xB9 3 2,742 Mar-14-2021, 06:49 AM
Last Post: buran
  keyboard module doesn't work in the microsoft version terminal of python. username 1 2,748 Feb-25-2021, 05:19 PM
Last Post: Larz60+
  Python Requests SSL Aussie 0 1,961 Jan-07-2021, 02:09 AM
Last Post: Aussie
  'urllib3' Module not found when import 'requests' spanz 5 9,963 Jan-06-2021, 05:57 PM
Last Post: snippsat
  why doesn't python look in two directions Kakha 21 6,311 Jan-01-2021, 11:24 PM
Last Post: jefsummers
  Python Requests Aussie 2 2,692 Dec-23-2020, 03:24 AM
Last Post: Aussie
  Python IDE doesn't see opencv-python package on my Jetson Nano sadhaonnisa 1 3,291 Oct-11-2020, 01:04 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020