Populating dropdown options/values via call to local API (was "Adding `Accept` or other headers to httpGet()")

I'd like to build a dropdown using the results of a local http request. I'll be hitting an ExpressJS server that's using serve-index to provide a directory listing of files. Unfortunately, unless I add Accept: application/json to the GET request, what's returned is formatted as html and not as the JSON array I require.

Has there been any interest in expanding httpGet() so that headers could be added? Are there use cases where JSON is not the desired format?

I guess expanding httpGet wouldn't be a problem. Meanwhile it's possible to do what you want with a custom module.

I'm looking into the Custom Module approach, @jean-emmanuel, which seems to avoid the header issue. I only need to get my file list once, when the server starts, so I assume I'd put the GET in init. I only want to pass along the array of filenames, like this–

const axios = nativeRequire('axios');

module.exports = {
  init: function() {
    // this will be executed once when the osc server starts

    axios.get('http://localhost:8889/transits')
      .then(function (response) {
        return(response.data);
      })
      .catch(function (error) {
        console.log(error);
      })
  },
}

–but I do not know how to wire this up so that the values for my dropdown are replaced by the array of filenames.

init is called only when the server starts, which usually happens before any client is connected. This would work better:

app.on('sessionOpened',  (data, client)=>{
  // fetch/process data
  //...

  // send data to an osc receiver (OSC{} syntax) in the appropriate property
  receive('/osc_receiver_address', data, {clientId: clients.id})
})

See also: Advanced syntaxes - Open Stage Control

This other answer put me on the right path:

app.on('sessionOpened',  (data, client) => {
  axios.get('http://localhost:8889/transits')
    .then(function (response) {
      receive('/EDIT', 'transit-choices', {"values": response.data}, {clientId: client.id})
    })
    .catch(function (error) {
      console.log(`getTransits error: ${error}`);
    })
})

response.data is a simple array of filenames, e.g., ["file_1", "file_2", …, "file_n"].

1 Like