3

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.

2
  • Possible duplicate of Vue.js log API endpoint response with vue-resource Commented Feb 2, 2017 at 11:07
  • Have you verified in your browser console for the actual Request that is sent to your API? Is it really provisioned with your headers? See last Leandro comment regarding apache httpd-vhosts.conf Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" Header set Access-Control-Allow-Headers "*, Authorization" Commented Feb 2, 2017 at 11:09

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.