4

I am using flowroute api and need to fetch data but not sure how to add authentication credentials i.e can be seen when I pass the query url in the browser How can I pass it in the javascript. I am using wix platform and adding javascript code as given below

// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import {fetch} from 'wix-fetch';
$w.onReady(function () {
    fetch('https://api.flowroute.com/v2/numbers/available?starts_with=800&limit=3?',{method: 'get',auth:{user:'28288282', pass:'099299292991'}})
      .then( (httpResponse) => {
       console.log(httpResponse.ok);
    if (httpResponse.ok) {

      return httpResponse.json();
    } else {
      return Promise.reject("Fetch did not succeed");



  }
  } )
  .then(json => console.log(json))
  .catch(err => console.log(err));

    //TODO: write your page related code here...



});

what is the correct method to pass username and password to the API so I can get a json response instead of current 401 Unauthorized response status

https://api.flowroute.com/v2/numbers/available?starts_with=800&limit=3 enter image description here

Updating my Answer to show you the Network Call Details Request & Response headers ae as given bwlow in the screenshot enter image description here enter image description here

enter image description here

harshal
  • 10,252
  • 3
  • 18
  • 23

1 Answers1

6

Try:

fetch('https://api.flowroute.com/v2/numbers/available?starts_with=800&limit=3?', {
  method: 'get',
  headers: {
    "Content-Type": "text/plain",
    'Authorization': 'Basic ' + btoa(username + ":" + password),
  },
})
agarwalankur85
  • 274
  • 2
  • 9
  • 1
    It's Base64 encoding, which means… not so secure – vol7ron Feb 20 '18 at 04:40
  • 1
    @harshal api.flowroute.com uses something called as `Basic Authorization` scheme. You need to pass the credentials in the request headers in the format: `'Authorization': 'Basic ' + `. `btoa` converts the string to base64 like @vol7ron mentioned. – agarwalankur85 Feb 20 '18 at 04:42
  • Hi @agarwalankur85, I tried applying Authorization code in the headers but it still gives false value.Please help and let me know what should I do to make it work as it works when I try it on browser. – harshal Feb 21 '18 at 03:18
  • @harshal - Could you check your network call and see if request header has Authorization header? Also check what error you are getting as part of the response of this call? – agarwalankur85 Feb 21 '18 at 03:43
  • Hi @agarwalankur85 I get 401 as statusCode and UnAuthorized as StatusText when I check the response. – harshal Feb 21 '18 at 07:10
  • Hi@agarwalankur85, I am sorry, but how can I check network call ? – harshal Feb 21 '18 at 07:11
  • @harshal try https://developers.google.com/web/tools/chrome-devtools/network-performance/reference if you are using Chrome. – agarwalankur85 Feb 21 '18 at 07:19
  • Hi @agarwalankur85, I have updated my answer,kindly have a look at it provide appropriate steps which needs to be taken to fix it,thanks – harshal Feb 21 '18 at 07:50