简体   繁体   English

如何在 Moleculer 中获取请求正文

[英]How do I get request body in Moleculer

According to the API documentation, to receive json as formData from a POST request, one must use body-parser.根据 API 文档,要从 POST 请求中接收 json 作为 formData,必须使用 body-parser。 I have declared it in the gateway service but I can still not receive the formData in my action.我已经在网关服务中声明了它,但我仍然无法在我的操作中收到 formData。

api.service.js 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在操作 service.insertUser 操作中,我应该将 req.body 作为 ctx.params 接收,但它始终为空

My users.service.js我的 users.service.js

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

have you tried giving params你试过给参数吗

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

图像

send post data with content-type:application/json使用 content-type:application/json 发送帖子数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Get Request In Express 中获取正文? - How Do I Get Body From Get Request In Express? 如何获取要从请求正文发送的文件作为参数? - How do I get the file to be sent from the request body as a parameter? 在POST请求触发之前,如何获取GET请求的响应主体的内容? 能做到这一点吗? - How do I get the contents of the response body of a GET request before the POST request fires? Can this be done with promise? 在Postman中,我如何从“获取请求”中获取响应正文,并将其放入经过细微更改的PUT请求中 - In Postman, how do I take a response body from a Get Request, and put it in a PUT request with minor changes 如何从我的 get 请求中从 MongoDB 文档中提取正文? - How do I extract the body out of the MongoDB Document from my get request? 我如何从身体获得输入 - How do i get input from body 我如何获得Angular 2中的Header Body? - How do i get the Header Body in Angular 2? 如何在Twilio SMS请求中指定unicode消息正文? - How do I specify a unicode message body in a Twilio SMS request? 如何在 express js 中解析多部分/混合请求正文? - How do I parse a multipart/mixed request body in express js? 如何在 mongoose 中将 ObjectId 作为请求正文发布? - How do I post ObjectId as request body in mongoose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM