[SOLVED] New Version Axios Post Request Responces 400 Bad Request

Hi all,

After a long time, I again started using strapi and created a new project by also following the instructions from Strapi Get Started. I created Colelction types as they are determined on Strapi’s website instructions and started to send requests by using API’s provided by strapi automatically.

GET method works well. But POST method did not work and gave 400 Bad Request error for 1 day. I had taken POST request example from official Strapi website like:

import axios from 'axios';

axios
  .post('http://localhost:1337/api/restaurants', {
    name: 'Dolemon Sushi',
    description: 'Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious',
    categories: [3],
  })
  .then(response => {
    console.log(response);
  });

However, there is a problem with this function which gives 400 error. It is that you need to re-arrenge JSON object a bit as:

Axios

  .post('http://localhost:1337/api/restaurants',
    {
      "data": {
        name: 'Dolemon Sushi',
        description: 'Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious',
        categories: [1]
      }
    }
  )
  .then(response => {
    console.log(response);
  });

If you send the POST request like this, 200 OK will return.
I think Strapi community still are not aware of the typo in the website that made me waste my hours… Hope to fix it on the website.

Have a nice coding to all!

8 Likes

Thank a lot for that, i’ve work on this problem for half a day and can’t figure what was wrong.

2 Likes

Bro thanks a lot, spent about 2 days on this bug, can’t be great-full enough

1 Like

Thank you a lot :grinning:

Thanks it was Very Useful :heart:

1 Like

WOW I spent a lot of time on this! Thanks for the solution!