POST requests failing/How to create entry via POST request in Strapi v4.0?

System Information
  • Strapi Version: 4.0
  • Operating System: macOS Catalina 10.15.7
  • Database: SQLite (default)
  • Node Version: v14.17.4
  • NPM Version: 6.14.14

Hi. I recently updated to v4 (new to Strapi, only worked a little with v3). I’m trying to create a content-type entry via a request sent from a frontend. From the documentation I understand that a controller must exist in the /api/my-content-type/controllers/my-content-type.js but there is not enough info for me to get the request working. (Controllers | Strapi Documentation)

I’m trying this step by step to understand, here are images of my code.

(Due to the limit on images I can include in the initial question, I’ll post the other images below.)

This is how the controller currently looks like (I’m logging ctx.request.body to see if the data gets through, which it doesn’t)…

…img #2 shows the request that is send from the frontend.
…img #3 shows the error I’m getting in the response ("Unexpected token o in JSON at position 1")

How does the controller need to be set up and the request to be formatted in order to create/update entries? Any help of guidance in the right direction would be great. I’ve looked through the docs and can’t find details on these questions.


1 Like

The second image mentioned in the question above:

(img #2, the request that is send from the frontend):

my-content-types is just a placeholder for the actual endpoint I’m working with

The third image mentioned in the question above:

(img #3, the response and error message):

I was able to fix the error by wrapping the data into another data key.


Hi @j0hSch,

Glad you solved it. For further reference, the information you need is here in the doc: REST API - Strapi Developer Docs

The :computer: Developer Resources section has a lot of useful guides!