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

408
Views
Api request Works in swagger and postman but doesn't work in js (using axios)

I have this POST api in swagger: enter image description here
enter image description here

when I send request in swagger it returns 200 status code

I also try the exact same request in postman and it also returns 200 status code so everything is good: enter image description here

but when it comes to js, I send the exact same request in js but server returns 400 status code with message: "title can not be empty"
but I'm sure that I'm sending title correctly.
this is my code:

export const createFood = async (title, description, file, price) => {
  try {
    const { data } = await client.post(`/Restaurant/food`, {
      title,
      description,
      "file.form_file": file,
      price: parseFloat(price),
    });

    return data;
  } catch (error) {
    return error.response.data;
  }
};

this is browser network log:
enter image description here
enter image description here

what am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In js, you are sending your data as an object. but you should send it as form-data like this:

export const createFood = async (title, description, file, price) => {
  const formData = new FormData();

  formData.append("title", title);
  formData.append("description", description);
  formData.append("file.form_file", file);
  formData.append("price", parseFloat(price));

  try {
    const { data } = await client.post(`/Restaurant/food`, formData);

    return data;
  } catch (error) {
    return error.response.data;
  }
};
about 4 years ago · Juan Pablo Isaza 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!