How should I customize controllers in order to sent POST request?

I am trying to make POST request to Strapi via Postman, with the following data: {
“data”: {
“type”: “test request”,
“start”: “01-01-2022”,
“end”: “10-01-2022”,
“description”: “test description”
}
} I am making the request to the following (custom) endpoint: http://localhost:1337/api/requests
but every time I try to make a request, Postman continue loading the request forever without any response.

I think that the problem is in the fact that I should customize the controllers inside src > api > [folder-name] > controllers > request.js file. Please let me know if I am wrong.

Can you give me some example about how should I customize request.js file in order to be able to make POST request?

For completely custom endpoint, you first need to create route. (see Routes - Strapi Developer Docs) and then point it to a your controller.

In you case, you could have something like src/api/<some name of your choice>/routes/<some name of your choise for the router>.js

With something along the lines of

module.exports = {
  routes: [
    {
      method: 'POST',
      path: '/requests', 
      handler: '<name of your controller>.<your action name>',
    },
  ]
}

And then adding a corresponding controller. See Backend customization - Controllers - Strapi Developer Docs.

Hope this helps.

goodhoko, thank you for your valuable time.

I followed Method 1 for creating controller inside src > api > [folder-name] > controllers > [file-name.js], and I putted the following code inside it:


"use strict";

const { createCoreController } = require("@strapi/strapi").factories;

module.exports = createCoreController("api::request.request", ({ strapi }) => ({
  // Method 1: Creating an entirely custom action
  async exampleAction(ctx) {
    try {
      console.log(ctx);
      ctx.body = "ok";
    } catch (err) {
      ctx.body = err;
    }
  },
}));

I also created route in routes folder.

Every time when I try to make POST request to the following endpoint trough Postman:
http://localhost:1337/api/requests

I am getting a string “ok” as a response. How could I change this into a data that user entered thought some input form? Or, let say, data that I am sending thought Postman?

thank you.

1 Like

I know this is a little old but you could send that information via query params