0

I am trying to make an API request to Wit.ai via javascript (ReactJS). My browser network tab shows the call fails with the message:

"error" : "Bad auth, check token/params"

However, that same call shows as successful in the Wit.ai logs. I've verified the credentials are correct and I can successfully cUrl the call via terminal.

Here's the call:

async action() {
    const resp = await fetch('https://api.wit.ai/message?v=20160526&q=hello', {
      method: 'GET',
      headers: {
        'Authorization': "Bearer " + accessToken
      },
      dataType: 'jsonp',
      mode: 'no-cors',
      credentials: 'include'
    }).then(resp => resp.json()).catch(e => console.log('Boo', e));
}
Pang
  • 9,564
  • 146
  • 81
  • 122
darrind
  • 31
  • 2
  • 5

1 Answers1

0

Since it's a JSONP request, you'll be having "Unexpected end of input" even though the request has been performed correctly. I'm not sure there's even any way to make it work without proxying the request through your app server. Anyway, for this kind of request, remove headers completely and move access token into query string as access_token query param:

await fetch(`https://api.wit.ai/message?v=20160526&q=hello&access_token=${accessToken}`, {
  method: 'GET',
  dataType: 'jsonp',
  mode: 'no-cors',
  credentials: 'include'
}).then(resp => resp.json()).catch(e => console.log('Boo', e));

Check out "Network" tab on your browser to see that the request has been resolved successfully, even though the catch block has been reached during the execution of this fetch call.

rishat
  • 8,206
  • 4
  • 44
  • 69