Hi PeterPeterLe wrote: ↑Sat Dec 31, 2022 10:07 amCorrection to the code in the opening post :Code: Select all
import json import requests def post_to_ba(endpoint, raw_json): headers = {'Content-Type': 'application/json'} response = requests.post(endpoint, data=raw_json, headers=headers) return response.text # Prompt the user for the selection name selection_name = input("Enter the selection name you require the ID for: ") # Make the request and store the response in a variable response = post_to_ba("http://localhost:9000/api/markets/v1.0/getMarkets", '{"dataRequired":["ID","NAME","MARKET_START_TIME","EVENT_ID","EVENT_TYPE_ID","MARKET_TYPE","SELECTION_IDS","SELECTION_NAMES"]}') # Parse the response text as JSON response_json = json.loads(response) print(response_json) # Initialize an empty list to store the matching selection names and IDs matching_selections = [] # Search for the selection in ALL markets for market in response_json['result']['markets']: for selection in market['selections']: if selection['name'].startswith(selection_name): matching_selections.append({'name': selection['name'], 'id': selection['id']}) # Print the name and ID of each matching selection if matching_selections: for selection in matching_selections: print(f"Selection name: {selection['name']}, Selection ID: {selection['id']}") else: print(f"No selections found with names starting with {selection_name}.")
I'm new to Python but I downloaded PyCharm as suggested and copying the above code but when I ran it I got the following error:
Traceback (most recent call last):
File "C:\Data\PycharmProjects\pythonProject2\main.py", line 2, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
I also tried testing your code from immediately above (your post of 7 January) and I get this error:
File "C:\Data\PycharmProjects\pythonProject1\main.py", line 3, in post_to_ba
response = requests.post(endpoint, data=raw_json, headers=headers)
^^^^^^^^
NameError: name 'requests' is not defined
In both cases it doesn't like 'requests'. Any suggestions as to what is wrong?
Thanks
Richard