Strapi routes return 404 Not found

I have a problem where all routes in my API return 404 Not found. I followed the Pull from Docker Hub section at strapi/strapi-docker.

What I did, apart from running the images, was creating a new Content type called post containing three fields. If I try GET /post (to get all posts added) I get unauthorized response error. This is expected at this stage. After I check Roles & Permissions to allow the Public role to use the find and findOne routes I instead get a 404 Not found response error even though data has been added.

The dev server does not use any prefix.

post routes for find and findOne looks as the following:

{
  "routes": [
    {
    "method": "GET",
    "path": "/post",
    "handler": "Post.find",
    "config": {
      "policies": []
    }
  },
  {
    "method": "GET",
    "path": "/post/:_id",
    "handler": "Post.findOne",
    "config": {
      "policies": []
    }
  }
}

There are not many options in the strapi interface to fiddle with so I'm not sure what else to try. I have tried a couple of other installations of strapi. Not sure if that could have messed it up but I vaguely remember trying out strapi/strapi-docker before and getting it to work.

Upvotes: 8

Views: 14167

Answers (3)

Sed
Sed

Reputation: 6401

The only reason for this is publish button, and sometimes it doesn't work if you have a convoluted component where one section errors but the screen does not scroll to it, therefore publish button seems to do nothing, logs show only a get request and you start pulling your hair...

Well don't, always check the page when publishing for any errors ie missing required field etc so that publish button works.

Upvotes: 0

DevKev
DevKev

Reputation: 6304

I've had this issue many times as well, even with published content.

Two additional causes could be:

  1. if your content type is posts try /posts as well as /api/posts (the 2nd usually works for me
  2. Under Settings > Roles (the one under USERS & PERMISSIONS plugins) make sure both Authenticated and Public user roles have find and findOne access to the content type.

One time I was getting the 404 even though I did this for Public...but because I was in the same browser as my logged in admin and needed to update it for Authenticated as well.

enter image description here

Upvotes: 6

mahatmanich
mahatmanich

Reputation: 11023

I have stumbled upon this issue many times, and the easiest answer usually is:

Have you published any of your posts?

Strapi has a publishing subsystem which is usually turned on upon the creation of any collection, single type or component. enter image description here

So when you create any content of any created type, the data is saved as draft and is not publicly available.

Here is my link collection type. It is saved, but not published.

enter image description here

So if you are trying to test an endpoint, but you only have one single data entry and it is set to draft, no data will show up.

enter image description here

No data!

enter image description here

In case of a single type that is not published

enter image description here

This will return a 404!

enter image description here

Publish and, voila ...

enter image description here

This could be one reason why strapi is sending you a 404 or only an empty array!

Upvotes: 10

Related Questions