嵌套json作为请求体在节点js中使用swagger-ui输入

时间:2017-06-12 11:40:07

标签: json node.js swagger-ui

我是节点js和swagger-ui的新手。

我想使用swagger-ui,

将下面的输入正文传递给我的网络服务

我能够传递普通的json,如:

{
"username":"abc",
"password":"******"
}

但我想使用swagger-ui向我的网络服务提供以下输入。

{
"data":
{"username":"abc",
"password":"******"
}
}

我的api-docs.json文件如下:

{
  "info": {
    "title": "Node Swagger API",
    "version": "1.0.0",
    "description": "Demonstrating how to describe a RESTful API with Swagger"
  },
  "host": "localhost:5002",
  "basePath": "/",
  "swagger": "2.0",
  "paths": {

    "/login": {
      "post": {
        "description": "user login",
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "userName",
            "description": "username to get userType",
            "required": true,
            "type": "string",
            "in": "formData"
          },
          {
            "name": "password",
            "description": "password to get userType",
            "required": true,
            "type": "string",
            "in": "formData"
          }



        ],
        "responses": {
          "0": {
            "description": "Access token has been expired. Please login again."
          },
          "200": {
            "description": "Successfully returns the response"
          },
          "400": {
            "description": "Invalid input Parameters"
          },
          "401": {
            "description": "Invalid Access Token"
          },
          "404": {
            "description": "No data found"
          },
          "500": {
            "description": "Internal Server Error"
          }
        }
      }
    }
  },
  "definitions": {},
  "responses": {},
  "parameters": {},
  "securityDefinitions": {},
  "tags": []
}

那么如何使用swagger-ui将以下输入传递给我的网络服务:

{
    "data":
    {"username":"abc",
    "password":"******"
    }
    }

任何帮助都会很明显。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我总是使用yml格式。试试这个。应该管用。现在,您将获得req.body中的详细信息。

{
"info": {
"title": "Node Swagger API",
"version": "1.0.0",
"description": "Demonstrating how to describe a RESTful API with Swagger"
},
"host": "localhost:5002",
"basePath": "/",
"swagger": "2.0",
"paths": {

"/login": {
"post": {
"description": "user login",
"produces": [
  "application/json"
],
"parameters": [
  {
    "name": "userDetails",
    "description": "username to get userType",
    "required": true,
    "in": "body",
    "schema": {
      "$ref": "#/definitions/User"
    }
  }
],
"responses": {
  "0": {
    "description": "Access token has been expired. Please login again."
  },
  "200": {
    "description": "Successfully returns the response"
  },
  "400": {
    "description": "Invalid input Parameters"
  },
  "401": {
    "description": "Invalid Access Token"
  },
  "404": {
    "description": "No data found"
  },
  "500": {
    "description": "Internal Server Error"
  }
}
  }
}
 },
 "definitions": {
 "User" : {
    "type":"object",
    "properties": {
      "data": {
        "type": "object",
        "properties": {
        "username": {
          "type": "string"
        },
        "password": {
          "type": "string"
        }
      }
    }
  }
 }
 },
 "responses": {},
"parameters": {},
"securityDefinitions": {},
"tags": []
}