question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Still cannot get Authorization to work

See original GitHub issue

I am currently developing a new web application with Vue.js and I am using axios for the REST API communication.

The API is on a server protected by basic auth and I cannot manage to get axios working with the Authorization header.

I tried it the way as described in https://github.com/axios/axios/issues/1769 and it did not work:


    const axiosInstance = axios.create({
      //method: 'GET',
      baseURL: process.env.API_BASE_URL,
      'Content-Type': 'application/json'
    });

    console.log(axiosInstance.defaults.headers);

// Alter defaults after instance has been created
    axiosInstance.defaults.headers.common['Authorization'] = 'Basic xxxx';
    axiosInstance.defaults.headers.common['X-ABC'] = "bla bla bla";

    axiosInstance.get(url)
    .then(response => {
      this.groups = response.data
    })
    .catch(e => {
      console.log(e);
      this.errors.push(e)
    });

I tried it like this and it did not work

    axios.get(url, [{
      headers: {
        "Authorization": "Basic xxxx"
      }
    }])
    .then(response => {
      this.groups = response.data
    })
    .catch(e => {
      this.errors.push(e)
    })

I tried it like this and it did not work

    axios({
      "url": url,
      "method": "GET",
      "headers": {
        "Authorization": "Basic xxx"
      }
    })
    .then(response => {
      this.groups = response.data
    })
    .catch(e => {
      this.errors.push(e)
    })

They all return 401 HTTP error.

I am hitting my limit frustrated because to what the problem is.

Can anybody help me give me a very clear example and tell me where the problem lies?

Looking at the console, it seems the headers do not get sent out!

Context

  • axios version: e.g.: v0.18.0
  • Environment: e.g.: v6.12.3, Mac OS SX

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
AnnoyedDevelopercommented, Sep 5, 2018

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. is displayed in the console.

Might this be a problem on the server side, that CORS is not enabled but chrome still prevents?

1reaction
gcoxcommented, Sep 5, 2018

@AnnoyedDeveloper axios has a documented option for basic authentication called auth. It is documented in the request config section of the readme.

From the readme:

  // `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
  // This will set an `Authorization` header, overwriting any existing
  // `Authorization` custom headers you have set using `headers`.
  auth: {
    username: 'janedoe',
    password: 's00pers3cret'
  },

So, something like this:

axios({
  method: 'get',
  url: url,
  auth: {
    username: 'your username',
    password: 'your password'
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

What if My Employee Doesn't Have Work Authorization?
Generally speaking, you are free to place on unpaid leave any employee or new hire who cannot provide proof of their employment authorization....
Read more >
Proving Work Authorization & Reverification
That is, the employer cannot require you to present another work permit with the new expiration date as long as you have other...
Read more >
Handling Employment Authorization Document Delays - SHRM
If USCIS is unable to issue the EAD within 90 days, it must produce an “interim” document to allow the foreign national to...
Read more >
Still No Work Permit?!?! - YouTube
Work permit delays have been happening across the board. We know how stressful it is for you to live in a state of...
Read more >
Employment Authorization Document (EAD), Explained
Prove you can legally work in the United States with form I-766, the Employment Authorization Document (EAD), also known as a Work Permit....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found