簡體   English   中英

如何在 Moleculer 中獲取請求正文

[英]How do I get request body in Moleculer

根據 API 文檔,要從 POST 請求中接收 json 作為 formData,必須使用 body-parser。 我已經在網關服務中聲明了它,但我仍然無法在我的操作中收到 formData。

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 }
    },
 },
};

在操作 service.insertUser 操作中,我應該將 req.body 作為 ctx.params 接收,但它始終為空

我的 users.service.js

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

你試過給參數嗎

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

圖像

使用 content-type:application/json 發送帖子數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM