0

I'm new to python & getting stuck in this... My code does some simple stuff, it requests an API endpoint and fetches the user name from the response, and makes a table as input we take mobile number .after a successful search it will give us the option to search again. the problem I faced when the invalid mobile number was given as input the code exit, the invalid response content-length is 160 so we can easily figure out the invalid request by the content-length, so i store the response content-length in z3 varibale . is there any way to check if the content-length is equal to 160 it will print data not found and give us chance to search again and send back to this line num = input ("Enter the mobile number :") . if content-length is less than 160 it will fetch the data which I have written in my code Also the response body for valid & invalid are different here is a valid response body: {"success":true,"data":{"acc_title":"Elon Musk"}} And the invalid one : {"success":false,"msg":"Sorry, unable to find beneficiary. This is not a customer account","log":""}

Here is the code :

from rich.console import Console
from rich.table import Table
from rich import print
import requests

p = True

while p == True:

    num = input ("Enter the mobile number :")
    x = num
    data = {'account_type': '11' ,'account_no': x } 

    cookies = {'token': '121212ad2122sdsd2323asas12312'}
    response = requests.post('https://pytest.com/api/user/get_user_info/', data , cookies=cookies) 

    z1 = response.json()
    z2 = response.headers
    z3 = z2["Content-Length"]
    z4 = z1["data"]
    z5 = z4["acc_title"]

    table = Table(title="[+] Found [+]", style="bold")
    table.add_column("Name", justify="right", style="cyan", no_wrap=True)
    table.add_row( z5 )
    console = Console()
    console.print(table)
    
    answer = input("want to search again? ")
    if answer == "yes":
            continue
    else:
        exit()

Need your help Thanks

mikhail
  • 27
  • 6
  • this will help [Size of raw response in bytes](https://stackoverflow.com/questions/24688479/size-of-raw-response-in-bytes) – deadshot Jul 30 '21 at 20:38
  • for every invalid request content length is fixed **160** – mikhail Jul 30 '21 at 20:45
  • what is your question? did you see the link I shared above? – deadshot Jul 30 '21 at 20:46
  • yes, I saw the link you shared, my question is if **z3** variable which is content-length gives us the value of 160 it will print us **data not found** and go to this line again `num = input ("Enter the mobile number :")` – mikhail Jul 30 '21 at 20:58
  • add this line after finding the `z3` value `if z3 == 160: continue` – deadshot Jul 30 '21 at 21:03
  • is there any way to go back `num = input ("Enter the mobile number :")` this line if the condition `if z3 == 160:` meets true Thanks – mikhail Jul 30 '21 at 21:33

1 Answers1

1

EDIT: Based on the comments below, I have changed the suggested code, so it checks the json-value assigned to z1 instead. (This contains a success-value, which returns a True or a False.)

from rich.console import Console
from rich.table import Table
from rich import print
import requests

p = True

while p == True:

    num = input("Enter the mobile number :")
    x = num
    data = {'account_type': '11', 'account_no': x}

    cookies = {'token': '121212ad2122sdsd2323asas12312'}
    response = requests.post('https://pytest.com/api/user/get_user_info/', data, cookies=cookies)

    z1 = response.json()
    z2 = response.headers
    if z1['success'] == False:
        print('Data not found.')
    else:
        z4 = z1["data"]
        z5 = z4["acc_title"]

        table = Table(title="[+] Found [+]", style="bold")
        table.add_column("Name", justify="right", style="cyan", no_wrap=True)
        table.add_row(z5)
        console = Console()
        console.print(table)

        answer = input("want to search again? ")
        if answer == "yes":
            continue
        else:
            exit()
Phorys
  • 379
  • 1
  • 5
  • There is an issue, The response body of a valid one and an invalid is different. "data" and "acc_title" are found in a valid response body and there is nothing like in an invalid response body , here is a valid response body: `{"success":true,"data":{"acc_title":"Elon Musk"}}` and an invalid : `{"success":false,"msg":"Sorry, unable to find beneficiary. This is not a customer account","log":""}` – mikhail Jul 30 '21 at 21:22
  • we need something that sends back to here `num = input("Enter the mobile number :")` if the condition **if z3 == 160:** get true . my bad maybe I can't elaborate the issue properly – mikhail Jul 30 '21 at 21:29
  • In the code I suggested it is checked whether z3==160, if that is true it does the print. Because this check happens within your while p == True:, then the code will skip the rest of the code from else: and return to the top of while p == True, in which it starts over. --> This new loop will begin with num = input("Enter the mobile number :") – Phorys Jul 30 '21 at 21:43
  • when I put an invalid number it show this error `File "uuu.py", line 23, in z4 = z1["data"] KeyError: 'data'` – mikhail Jul 30 '21 at 21:51
  • OK. The reason for that is that the check == 160, is not 160 long and thus the check fails. I am posting an edit to the code, which utilizes the json from your first comment instead. – Phorys Jul 30 '21 at 22:02