1

According to the API documentation, to receive json as formData from a POST request, one must use body-parser. I have declared it in the gateway service but I can still not receive the formData in my action.

api.service.js

module.exports = {
 name: "api",
 mixins: [ApiGateway],

 settings: {
    port: process.env.PORT || 3000,

    routes: [{
        path: "/api",
        aliases: {
          "POST users": "users.insertUser",
        },
        //The API Documentation recomends using the body-parser here
        bodyParsers: {
            json: true,
            urlencoded: { extended: true }
        },
    }],

    // In some example they also set the body-parser here
    bodyParsers: {
        json: true,
        urlencoded: { extended: true }
    },
 },
};

In the actions service.insertUser action I am supposed to receive the req.body as ctx.params, however it is always empty

My users.service.js

    actions: {
    insertUser: {
        handler(ctx) {
            this.logger.info("posting", ctx.params); // -> prints {} instead of the formData
        }
    }
fabrigeas
  • 11
  • 1
  • 1

2 Answers2

0

have you tried giving params

insertUser: {
            auth: "required",
            params: {
                function_id: { type: "string" },
                role_id: { type: "string" },
            },
            handler(ctx) { ctx.params.role_id 

img

Azat
  • 23
  • 5
0

send post data with content-type:application/json

Avalon Lu
  • 19
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – jasie Aug 23 '22 at 07:24
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32514531) – Shashank Gb Aug 24 '22 at 05:21