How can I pass parameters to my custom API controller in Strapi v4?

System Information
  • Strapi Version: 4.0.5
  • Operating System: macOS
  • Database: MySQL
  • Node Version: 16

I have a route:

module.exports = {
    routes: [
        {
            method: 'POST',
            path: '/test,
            handler: test.post_test',
        },
    ]
}

… and a controller:

module.exports = {
    async post_test(ctx, next) 
    {
        const user = ctx.state.user		// user is ok … filled with valid data
        console.log(">>>>> ctx.params", ctx.params)
        console.log(">>>>> ctx.request.query", ctx.request.query)
        console.log(">>>>> ctx.request", ctx.request)

        ctx.body = "ok" 
    }
};

When I call the API http://localhost:1337/api/test with a valid bearer and the JSON body

{
    "data": {
        "param1": "test1",
        "param2": "test2"
    }
}

I cannot access the sent parameters in the controller.
This is what I get in the console window:

>>>>> ctx.params {}
>>>>> ctx.request.query {}
>>>>> ctx.request {
  method: 'POST',
  url: '/api/test',
  header: {
    authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MywiaWF0IjoxNjQzMjEzOTY2LCJleHAiOjE2NDU4MDU5NjZ9.1M8vZ2lcSPHAPG8EtTaEkZ7KQbNcx4wAvyVmVbtWogI',
    'content-type': 'application/json',
    'user-agent': 'PostmanRuntime/7.28.4',
    accept: '*/*',
    'cache-control': 'no-cache',
    'postman-token': 'ec442743-9586-4e13-9ae8-4de448c0856f',
    host: 'localhost:1337',
    'accept-encoding': 'gzip, deflate, br',
    connection: 'keep-alive',
    'content-length': '76'
  }
}

Does anyone have an idea where the problem is?

regards

I think you can access the post content from ctx.request.body.

That’s it!
Thank you Aashis

hi do i need to create plugin for custom Api, when i do this in strapi 4 i get errors.