How to provide login and password to access an api with vueJs and Axios?

Asked

Viewed 799 times

0

I’m getting an error: Request failed with status code 401 when trying to connect to an api using Vue and Axios, I am completely new to consuming api, in case it is a Django Rest, which is the correct syntax to inform the login and password using Axios, maybe it is this trexo

<script>
import axios from 'axios';

export default {
    data() {
        return {

            errored:false
        }
    },
    mounted() {

        axios
            .post('https://xxx.com.br/api/', {}, {
                auth: {
                    username: 'teste',
                    password: 'teste123'
                }
            })
            .then(response => (this.info = response))
            .catch(error => {
                console.log("o catch pegou " +error);
                this.errored = true;
            })
            .finally(() =>{
                this.loading = false;
            });

    }
}
</script>
  • If the console log.() brings the data correctly, the request is being made correctly, the error 401 is usually triggered when you are not allowed access or for some reason permission and permission data does not confer.

  • The http 401 status means it has not been authorized. You need to see how this API works for authentication (read its documentation). Usually, you need to send something to recognize that you are you, and for that it is necessary to be POST and see if you need to send login and password, or just an authentication token. For example, some API you first authenticate to another URL on the login page and a TOKEN, and later in each request for the API, you need to send this TOKEN (as if it were a login and password).

  • I probably need to send login and password because they sent me these, but the question is what is the syntax for sending login and password using Axios/Vue? It’s a test that I was sent to a company vacancy, I don’t even think there’s documentation to consume the api, but I just requested the documentation from the recruiter.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.