[Custom API] Using a URL from a variable/constant in urlfetch

Hello. I would like to use urlfetch with a URL that is contained inside a variable. How can I achieve it?

This code works (returning the desired information from the website)

!commands edit !test $(eval ‘$(urlfetch http://test.com)’)

This one doesn’t (returning “Right-hand side of ‘instanceof’ is not an object” error message)

!commands edit !test $(eval const my_url = ‘http://test.com’; ‘$(urlfetch my_url)’)

By the way, I’m using eval because the actual !test command will contain more code, but the relevant problem is this one, trying to use a variable with urlfetch.

Thanks for your help!

You have to put an eval inside the urlfetch:

!commands edit !test $(urlfetch $(eval my_url = 'http://test.com'; my_url))

Or if you also want to process the urlfetch result with another eval:

!commands edit !test $(eval response = `$(urlfetch $(eval my_url = 'http://test.com'; my_url))`; response)

If you need more than two levels of nesting, you’ll have to make !test an alias to another command. !test can pass data to the alias, and the alias can access it with the query variable.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.