Set Header via Ajax through eval()

Hi there, initially i wanted to build a command that returns the how many commands a channel has. Since this functionality is not provided natively i figured i could make use of UrlFetch. Checking the Commandpage for a given Channel in Chromes Inspector revealed that a request like this is needed:

curl ‘https://api.nightbot.tv/1/commands’ -H ‘nightbot-channel: 5c7288c4cc39145dcc8b5ebe’
5c7288c4cc39145dcc8b5ebe being my nightbot-channel-id

I quickly discovered, that UrlFetch does not have support for manually set headers.

I’m not a JS person but figured this could surely be done with the help of eval() which Nightbot supports.

I got things down to an ajax request in the style of:

$.ajax({
type : “GET”,
url : “https://api.nightbot.tv/1/commands”,
beforeSend: function(xhr){xhr.setRequestHeader(‘nightbot-channel’, ‘5c7288c4cc39145dcc8b5ebe’);},
success : function(result) {
console.log(result._total)
}
})

which i call from a pastebin through like so:

$(eval ‘$(urlfetch https://pastebin.com/raw/carvKLj4)’)

i guess the issue here is some escaping or that ajax simply won’t work with however eval() is implemented within Nightbot. It would be great if someone could help me figure out where my oversight is as i’m absolutely new to Javascript and Webdevelopment really isn’t my strong suit.

For this specific project i would be happy to just get the number of commands displayed, but in general i’d be more interested in being able to send Headers with my requests as these are mandatory with many APIs.

You can’t actually make async requests (or any requests at all) within our eval commands, as all those are blocked. The only way to obtain data from a remote source is with urlfetch. We may explore something more robust to account for use cases like this in the future.

too bad…
so the only way i could pull this off is by having my pc or any server constantly poll the API and host a webpage that’s returning a JSON or textfile with the current values.

I’d really appreciate if you’d allow setting Headers so APIs can be easily queried with urlfetch.

Thanks for the reply!

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