Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!