https://xxx.com/_api/auth/lalala How do I make a post request to this endpoint the json…
- User
https://xxx.com/_api/auth/lalala
How do I make a post request to this endpoint the json payload being this: "user: 111111"
where 111111 will be replace with the code i enter earlier, in python all of this
- Assistant
import requests import json
url = "https://xxx.com/_api/auth/lalala" code = input("Enter code: ")
payload = {"user": code} headers = {"Content-Type": "application/json"}
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response.text)
- User
allow to pass 1 cookie called "abc.xyz" with the value of that cookie being whatever is stored in the variable "jkl"
- Assistant
import requests import json
url = "https://xxx.com/_api/auth/lalala" code = input("Enter code: ") jkl = "example_cookie_value"
payload = {"user": code} headers = {"Content-Type": "application/json"} cookies = {"abc.xyz": jkl}
response = requests.post(url, data=json.dumps(payload), headers=headers, cookies=cookies)
print(response.text)
- User
when i print response.text, it will either print out {"success":true} or {"success":false}
can you make it so if it prints out success false it keeps trying again until it says true?
- Assistant
import requests import json
url = "https://xxx.com/_api/auth/lalala" code = input("Enter code: ") jkl = "example_cookie_value"
while True: payload = {"user": code} headers = {"Content-Type": "application/json"} cookies = {"abc.xyz": jkl}
coderesponse = requests.post(url, data=json.dumps(payload), headers=headers, cookies=cookies) check_response = json.loads(response.text) if check_response.get("success") is True: print(response.text) break else: print("Retrying...") - User
while True: attemptR = requests.post(rTEndpoint, data=json.dumps(payload), headers=headers, cookies=cookies) checkSuccess = json.loads(attemptR.text)
codeif checkSuccess.get("success") is True: print("Successfully!") input("Press enter to exit..") sys.exit() elif checkSuccess.get("success") is False: print("Failed") continueFor some reason when success is false, it continues to print failed, what did i do wrong?
- Assistant
In the provided code snippet, it appears that there is a logical error in the condition check for the "success" key in the response JSON. The
json.loadsmethod converts the response text to a dictionary, and thus, the conditioncheckSuccess.get("success") is Falsewill always evaluate toFalseif the "success" key exists in the response JSON and its value isFalse.To correct this issue, you can modify the condition to check for the absence of the "success" key or check if the value is False. Here is the corrected code snippet:
pythonwhile True: attemptR = requests.post(rTEndpoint, data=json.dumps(payload), headers=headers, cookies=cookies) checkSuccess = json.loads(attemptR.text) if "success" in checkSuccess and checkSuccess["success"] is True: print("Successfully!") input("Press enter to exit..") sys.exit() elif "success" in checkSuccess and checkSuccess["success"] is False: print("Failed") continue