How can i pass parametar from my end point to controller

System Information
  • Strapi Version: 3.6.8
  • Operating System: Win10
  • Database: mariadb
  • Node Version: v12.18.3
  • NPM Version:
  • Yarn Version:

Hello all,

i have custom router

{
“method”: “GET”,
“path”: “/MyScript/get”,
“handler”: “MyScript.MyScript”,
“config”: {
“policies”: []
}
},

and would like to pass parametars to my script “MyScript” i would like to call “http://localhost:1340/MyScript/get?par0=1313&par1=1231&par2=3134
and be able to get values of par0, par1, par2 in controller. but every time i get is 403 return.
route is allowed in strapi settings.

does anyone know something about or have done anything?

Hi @WardenCommander,

For starters we should try to fix the 403 status code. It means you are not allowed to access the route. You can change that in the permissions for your role.
See the docs on how to do that:

Hello @boaz

thank you for reply,

I checked my route and its active.
My question is how to define route that i can access parameters in query.

If i define route as:

{
“method”: “GET”,
“path”: “/MyScript/get/:id”,
“handler”: “MyScript.MyScript”,
“config”: {
“policies”: []
}
},

then there is no problem.

But i would like my path to be: “path:/MyScript/get” as i would like to pass 1 or 4 parameters into my function and return xml

You can access the :id inside the controller by using ctx.params to get the parameters
so

const { id } = ctx.params;

this would extract the :id that was requested if you are sending this an array you can desctructure it but I would then send it with more params

2 Likes

Hello @Eventyret

I know how to get id what i need is other parameters, here is a image of interface i need to have, disregard “php” part

@WardenCommander
You can try using ctx.request.query to get the query params.

Like this:

const { objectid, datefrom, dateto, persons } = ctx.request.query;
4 Likes

thank you, this solve it :slight_smile:

@WardenCommander Awesome!
Feel free to mark my answer as the solution to your question :slight_smile:

1 Like

so i am trying to get a custom strapi controller where i want to filter based on CompanyName
module.exports = createCoreController(‘api::partner.partner’, ({strapi}) =>({
async customFind(ctx){
const { query } = ctx.request.query;
const entity = await strapi.service(‘api::partner.partner’).find({query});
return entity
},

and my route is
module.exports={
routes:[
{
method:‘GET’,
path: ‘/search’,
handler: ‘partner.customFind’,
config:{
auth:false,
}
}
]
}

BUt when i am trying
http://localhost:1337/api/partners/search?ComanyName=XYZ
i am getting 404 notfound error

i have the selected find and findone in the role and public