繁体   English   中英

昂首阔步的Node.js Express

[英]swagger POST node.js express

我使GET服务在Swagger中(通过Node.js)工作,但无法使POST工作。 有人可以指出我在以下testPost服务中出错的地方吗?

我的服务定义

 "/people/testPost": {
  "post": {
    "summary": "write the test POST.",
    "description": "Test",
    "operationId": "myService",
    "consumes": [
        "application/json"
      ],
    "tags": [
      "test"
    ],
    "parameters": [
    {
        "name":"body",
        "paramType":"body",
        "description": "body for the POST request",
        "required":false
      }

    ]

在服务中(文件名= myService.js),我正在尝试

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:true}));


app.post("/people/testPost", function (req, res) {
console.log("I am in the testPost service);  //this gets printed
console.log("The req body", req.body)   //says 'undefined'
});

这是我用于POST的CURL命令

curl -H "Content-Type: application/json" -X POST -d '{"arg1":"open", "arg2":"IVR","arg3":"", "arg4" :"1234567"}' http://localhost:8016/people/testPost

希望任何使用Swagger和Node的简单POST请求(我所需要的只是如何在json文件中配置我的“ body”)都有望使我前进

请其中一些查看我的“参数”和curl POST,看看我需要在哪里进行更改/更正。

因为您正在接收JSON请求,所以您需要一个适当的处理程序。 对于JSON,您应该使用app.use(bodyParser.json()); ,请参阅模块页面上的示例。

app.use(bodyParser)不需要顺便说一句。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM