Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Testing fetch()

Here's a lazy way of testing your companion fetch() code:

  • Go to request catcher, enter a subdomain for yourself (eg, your name), and 'get started'.
  • Optional: at a command prompt, enter the curl command provided, to verify that the request catcher server is working. (You may need to replace the single quotes with double quotes. I had more luck using a WSL prompt instead of a cmd prompt due to certificate weirdness.)
  • In your Fitbit companion/index.js:
const subdomain = 'my-subdomain'
const url = `https://${subdomain}.requestcatcher.com/test`

const data = "G'day."
let fetchInit = {method: 'POST', body: data}

fetch(url, fetchInit)
  .then(function(response) {
    if (response.ok) {
      response.text().then(text => console.log(`Server response: OK (${text})`))
    } else {
      response.text().then(text => console.log(`Server response: not OK (${text})`))
    }
  })
  .catch(function(err) {
    console.log(`fetch error (${err}).`)
  })
  • Replace my-subdomain with whatever subdomain you specified in request catcher.
  • Specify the access_internet permission in your package.json. (This is not necessary if testing on the Fitbit simulator.)
  • Build, install and run your Fitbit app.
  • Check Fitbit console logs for results.
  • Check your request catcher subdomain page to see if it received the request from your app.

If you're having troubles with fetch(), this can be useful to isolate whether the problem is with your Fitbit (client) code, or with your server.

You can also use request catcher to test the various fetch() capabilities documented in the Fitbit fetch API.

You probably won't be able to use request catcher for more than simple testing; you'll still need to provide your own server for that.

Peter McLennan
Gondwana Software
Best Answer
0 REPLIES 0