Advertisement
homeworkhelp111

json

Jul 12th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. import requests
  2. import json
  3. from dotenv import dotenv_values
  4. def test_get_request(username,password):
  5. url = "http://api.your-url.com"
  6.  
  7. response = requests.get(url,verify=False, auth=(username,password))
  8. # If the request was successful, response.status_code will be 200
  9. print(f"Status code: {response.status_code}")
  10.  
  11. # Assuming that the response is JSON, you can convert the response to a Python dictionary using .json()
  12. if response.status_code == 200:
  13. data = response.json()
  14. obj = json.loads(data)
  15. json_formatted_str = json.dumps(obj, indent=0)
  16. print(json_formatted_str)
  17. #print(f"Content:\n{data}")
  18. else:
  19. print(f"Response content is not printed because the request was not successful.")
  20.  
  21. # Check if the request was successful
  22. if response.status_code == 200:
  23. return True
  24. else:
  25. return False
  26.  
  27.  
  28. config = dotenv_values(".env")
  29.  
  30. # Test GET API
  31. test_result = test_get_request(config['username'],config['password'])
  32.  
  33. if test_result:
  34. print("The GET request to the API was successful.")
  35. else:
  36. print("The GET request to the API was not successful.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement