Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
Vue 3 customize axios get,put,post

I want to customize my get,post,put functions with axios. After doing the axios.create() operation, when I do the get operation, I want it to perform the then and catch operation there every time.

import axios from "axios";

export default (apiName = "") => {
    let headers = {
        "Content-Type": "application/json"
    };

    let token = JSON.parse(localStorage.getItem("token") || "{}");

    if (token && token !== "") {
        headers.Authorization = `Bearer ${token.AccessToken}`;
    }
    const instance = axios.create({
        baseURL:
            apiName === ""
                ? process.env.VUE_APP_API_URL
                : process.env.VUE_APP_API_URL + apiName,
        withCredentials: false,
        headers: headers
    });
    return instance;
};

axios.post('/api/Export/CopyShippingPlan/', plan)
            .then(handleResponse)
            .catch((error) => {
                console.log(error);
            });

I want to define axios.post here after axios.create and have then and catch action wherever I call axios.post action. So I don't want to have to use then and catch where I use axios every time.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think that you're looking for interceptors:

  • response interceptor will be called on every axios response
  • request interceptor will be called before every axios request

Example:

const ApiService = axios.create()

ApiService.interceptors.response.use(
    config => {
        //your code

        return config
    },
    error => {
        console.log(error)
        return Promise.reject(error)
    })


ApiService.interceptors.request.use(
    config => {
        //your code
        
        return config
    },
    error => {
        console.log(error)
        return Promise.reject(error)
    })

After setting them up you don't need to do anything special just call axios.get, axios.post, etc.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda