Get a 401 error with POST and PUT Laravel passport

2019-08-26 09:04发布

问题:

I am working on a Vue application with a seperate Laravel back-end API. The back-end has Laravel passport that requires an access token when doing database calls.

Normally everything goes right, I can get data back from the database but for some reason, 2 of my calls gets errors, the POST en PUT. I don't know why I get unauthenticated (401) from laravel passport back, while my get request is going well. Also, both POST and PUT are going fine in the postman application.

The get request

   getSuppliers() {
        axios.get(`${this.$API_URL}/api/v1/relations`, {
            headers: this.headers,
        })
            .then((response) => {
                this.loaded = true;
                this.relations = response.data.data;
            })
            .catch(error => console.log(error));
    },

The post request

    axios.post(`${this.$API_URL}/api/v1/relations`, {
        headers: this.headers,
        data: {
            company_name: this.supplier.company_name,
                    language_id: this.supplier.language_id,
                    supplier_type_id: this.supplier.supplier_type_id,
                    email: this.supplier.email,
                    website: this.supplier.website,
                    recognition_number: this.supplier.recognition_number,
                    street: this.supplier.street,
                    house_number: this.supplier.house_number,
                    zipcode: this.supplier.zipcode,
                    city: this.supplier.city,
                    country: this.supplier.country,
                },
            })
                .then((response) => {
                    console.log(response);
                    // retrieve the id
                    // push the user to the show of the retrieved id
                })
                .catch(error => console.log(error));

Helper functions for access token

function getHeaders(token) {
    return {
        Accept: 'application/json',
        Authorization: `Bearer ${token}`,
    };
}

function getToken() {
    const oauth = JSON.parse(localStorage.getItem('oauth') || '{}');
    if (oauth.access_token) {
        return oauth.access_token;
    }
    return false;
}

Somebody out there that had the same problem or similair?

回答1:

After some digging, going through my other code and requests I find a solution that fixed it for me.

Instead of

axios.post(`${this.$API_URL}/api/v1/relations`, {
    headers: this.headers,
    data: {
        company_name: this.supplier.company_name,
                language_id: this.supplier.language_id,
                supplier_type_id: this.supplier.supplier_type_id,
                email: this.supplier.email,
                website: this.supplier.website,
                recognition_number: this.supplier.recognition_number,
                street: this.supplier.street,
                house_number: this.supplier.house_number,
                zipcode: this.supplier.zipcode,
                city: this.supplier.city,
                country: this.supplier.country,
            },
        })
            .then((response) => {
                console.log(response);
                // retrieve the id
                // push the user to the show of the retrieved id
            })
            .catch(error => console.log(error));

I had to do

axios({
    method: 'post',
    url: `${this.$API_URL}/api/v1/relations`
    headers: this.headers,
    data: {
        company_name: this.supplier.company_name,
                language_id: this.supplier.language_id,
                supplier_type_id: this.supplier.supplier_type_id,
                email: this.supplier.email,
                website: this.supplier.website,
                recognition_number: this.supplier.recognition_number,
                street: this.supplier.street,
                house_number: this.supplier.house_number,
                zipcode: this.supplier.zipcode,
                city: this.supplier.city,
                country: this.supplier.country,
            },
        })
            .then((response) => {
                console.log(response);
                // retrieve the id
                // push the user to the show of the retrieved id
            })
            .catch(error => console.log(error));

I don't see any difference except the style, so I don't know exactly why the second style is working and the first one is not, especially for the put and post type of requests.