Marcus Christiansen
Marcus Christiansen

Reputation: 3197

Vue Resource with Basic Authentication

I wrote a method in Vue 2.0 with Vue Resource which connects to an API with Basic Authentication.

getCountries: function()
{
      options = {
          headers: 
          {
            'type'                        : 'GET',
            'Authorization'               : 'Basic c3VyZWJ1ZGR5LWFwaS11c2VyOkFwaTQzMjJTdXJlYg==',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Allow-Methods': 'GET',
            'Access-Control-Allow-Origin' : '*',
            'dataType'                    : "json"
          }
      }
      this.$http.get('http://surebuddy.azurewebsites.net/Api/Products', [options])
      .then((response) => {
        console.log(response.body);
      }, (error) => {
        console.log(error);
      });
}

When I run this in the browser I simply get a "403 (Forbidden)" error message in the console.

With these authorisation credentials in Postman I can perfectly connect and receive data. I have a feeling that I'm incorrectly passing the Authorization in the header.

Upvotes: 3

Views: 6674

Answers (1)

Luke
Luke

Reputation: 2400

Try doing it this way:

var options = {
    url: 'http://surebuddy.azurewebsites.net/Api/Products',
    method: 'GET',
    headers: 
    {
        Authorization: 'Basic [your auth key in encoded in base64 here]'
    }
}
this.$http(options).then((response) => {
            //...
});

I've tested it locally and it worked with your auth key and url. Consider replacing your auth key with a placeholder and change your basic auth credentials.

Upvotes: 6

Related Questions