WebPagetest Forums
Defining custom hero via the API - Printable Version

+- WebPagetest Forums (https://www.webpagetest.org/forums)
+-- Forum: WebPagetest (/forumdisplay.php?fid=7)
+--- Forum: General Discussion (/forumdisplay.php?fid=25)
+--- Thread: Defining custom hero via the API (/showthread.php?tid=16050)



Defining custom hero via the API - mwoodward - 05-02-2020 08:59 AM

I'm having trouble defining a custom hero via the API. I can get everything to work OK using the UI, but not the API. Here's my Python code:

# Prepare the post data
post_data = {'k': API_TOKEN,
'location': location,
'url': url,
'runs': 1,
'fvonly': 1,
#'debug': 1,
'keepua': 1,
'f': 'json',
'video': True,
'script': script,
'custom': '\n'.join(custom),
'heroElements': {"MWTEST":".product"}
}

# Make the request
response = requests.post(BASE_URL,
data=post_data,
timeout=10)

custom is a script - it runs OK and the output is in the results. heroElements works fine when I put the dict in the UI. But when I run this code, there's no custom hero at all in the results and nothing shows up in debug.

Has anyone got any advise on custom heroes via the API?


RE: Defining custom hero via the API - mwoodward - 05-13-2020 11:55 PM

I figured out how to do this. The solution is to use json.dumps(), so the dict becomes:

'heroElements': json.dumps({"MWTEST":".product"})

I can confirm that this works - meaning you can define custom heroes through the API.