Python Forum

Full Version: Python requests module doesn not return status_code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
remove the try/except and post the full traceback you get
(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
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
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.
(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