If you have this word in the Curl answer, print "OK"

Asked

Viewed 88 times

1

I made a Python URL to check some information of my client, everything is going well, the answer of my api is json, I would like to know how I do for if the api has, for example "userid", it samples a "OK" in the console.

 import requests


url = "https://minhaapi.com/v1/login/username"

querystring = {"countryCode":"BR"}

payload = "username=user&password=teste123&clientVersion=2.4.9-undefined"
headers = {
    'Content-Type': "application/x-www-form-urlencoded",
    'x-requested-with': "XMLHttpRequest",
    'x-tidal-token': "wdgaB1CilGA-S_s2"
    }

response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)



Resposta da minha API(Quando o usuário loga com as informações no curl): 



{"userId":57261722,"sessionId":"5b1ada5b-addf-4804-a55e-a12f36959ff1","countryCode":"BR"}

I tried this code, but I couldn’t:

for url in response.text:
        if 'userId' in url.lower():
            print ("OK")

I just want you to print on the "OK" screen when Curl displays the json response above (that’s when the user logs in), and if a different message appears it prints "Error" on the screen

  • json, friend....

  • Please undo your edit and return the question to its original form. The original question is totally different and the accepted answer reflects this. It is important to leave questions in their original forms because other people with similar doubts can reach them through searches. It’s okay to create a new question to ask a different question.

  • Ready friend, could you try to help me in the other question ? Thank you

1 answer

1


for url in response.text:
        if 'userId' in url.lower():
            print ("OK")

Here you see if userId is in url.lower(). This will never work in True because the I in userId is uppercase, and you compare that to the lowercase version of url.

url also won’t be a string; response.text is a string, and when iterated over a string with a for, you get individual characters. I recommend you try putting one print(url) before the if to get an idea of what’s going on.

Since your API returns a JSON, you don’t have to deal with response.text, can use the method .json() to turn the answer into a Python dictionary. From there, you can check whether userId is a key in the dictionary:

if 'userId' in response.json():
    print('OK')
else:
    print('ERRO')

You should also use a dictionary to pass your payload, not a string:

payload = {'username': 'user',
           'password': 'teste123',
           'clientVersion': '2.4.9-undefined'}
  • Thank you, Pedro. Just one more thing, I have a list in. txt in the email|password format of my clients and want to check the integrity of some registrations. I wanted to create a variable that would pick up line by line and change the email and password on the payload of the URL, understood ? For example, it reads the . txt and test the records one by one, line by line.

  • Type: [email protected]|cliente1

  • [email protected]|cliente2 and so on

  • @Gennie can create a new question and include what you’ve tried and why it didn’t work?

  • I will edit the topic.

  • @Gennie, that question is already answered and it’s on a different subject. It’s okay to create a new question, since the two are unrelated.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.