Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda