Bulk import Synthetic simple browser monitor

I am attempting to import the synthetic name and URL from a text file using the Synthetics API and Python. It works but only creates the Synthetic for the last line in the text file. I have one name and URL on each line. It seems that it has an issue with the return at the end of each line where there isn’t a return on the last line. I get a 400 bad request for all but the last synthetic. Anyone know how I should format this text file?

python script:
import requests
api_key = ‘my_api_key’

def create_synthetic(monitor_name, monitor_url):
url = “https://synthetics.newrelic.com/synthetics/api/v3/monitors
payload = “{“name” : “” + monitor_name + “”, “type” : “browser”, “frequency” : 15, “uri” : “” + monitor_url + “”, “locations” : [“AWS_US_EAST_2”, “AWS_US_WEST_2”, “AWS_US_WEST_1”, “AWS_US_EAST_1”, “AWS_CA_CENTRAL_1”], “status” : “ENABLED”, “slaThreshold” : “7.0”}”
print(payload)
headers = {
‘X-Api-Key’: “” + api_key + “”,
‘content-type’: “application/json”,
‘Cache-Control’: “no-cache”,
}
response = requests.request(“POST”, url, data=payload, headers=headers)
print(response.text)

with open(‘Hosts.txt’) as f:
for line in f:
currentline = line.split(",")
monitor_name = currentline[0]
monitor_url = currentline[1]
print(monitor_name)
print(monitor_url)
create_synthetic(monitor_name, monitor_url)

example of how the text file is formatted:
name,url
name,url
name,url

Help is appreciated!

-Ed

Resolved my issue by adding line = line.rstrip() under for line in f:. This removed the carriage return from each line in the file.

1 Like

Hi there @support_omnigo -

I am so glad you figured this out and very grateful that you shared the resolution here in the community so that others can learn too.

1 Like