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

173
Visualizações
How to switch from Fetch to Axios

i am refactoring a code and need to switch from fetch to axios. i got following code:

const createAttachment = async (formData: FormData): Promise<boolean | string> => {
  try {
    const response = await fetch(API_URL, { method: 'POST', body: formData });
    const resultText = await response.text();

    if (response?.ok) {
      return resultText;
    }

    // sending fails
    captureMessage(
      `Kontaktní formulář selhal při nahrávání přílohy: ${JSON.stringify(resultText)}`,
    );
    return false;
  } catch (error) {
    captureMessage(`Kontaktní formulář selhal při nahrávání přílohy: ${error}`);
    return false;
  }
};

i just cant resolve how to get response.text() in axios ?

This is how i am using axios:

const api = axios.create({baseURL: env.STRAPI_URL})

const createAttachment = async (formData: FormData): Promise<boolean | string> => {
  try {
    const response = await api.post(DAKTELA_FILE_API, formData);
    const resultText = await response.data

    if (response.statusText === "OK" && response.status === 200) {
      return resultText;
    }

    // fail to send
    captureMessage(
      `Kontaktní formulář selhal při nahrávání přílohy: ${JSON.stringify(resultText)}`,
    );
    return false;
  } catch (error) {
    captureMessage(`Kontaktní formulář selhal při nahrávání přílohy: ${error}`);
    return false;
  }
};
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

To make the response type of axios with text, you just need to pass responseType: 'text' and transformResponse: undefined to the options object.

const createAttachment = async (formData: FormData): Promise<boolean | string> => {
  try {
    const resultText = await axios(API_URL, { method: 'POST', body: formData, responseType: 'text', transformResponse: undefined });

    return resultText;
  } catch (error) {
    captureMessage(`Kontaktní formulář selhal při nahrávání přílohy: ${error}`);
    return false;
  }
};

about 4 years ago · Santiago Trujillo Relatório

0

The response data will be in the 'data' field of resolved response https://github.com/axios/axios#response-schema

You can post the formData using Axios in this way, without the extra await for extracting the text.

const response = await axios.post(API_URL, formData);
const resultText = response.data;
about 4 years ago · Santiago Trujillo 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