首页 > 解决方案 > Swagger 无法识别 x-www-urleconded 参数

问题描述

我有一个关于 2.0 版的招摇文档,我将路径消耗为application/x-www-urlencoded但是当我尝试请求时,我收到以下错误。

{
"message": "Request validation failed: Parameter (username) is required",
"code": "REQUIRED",
"failedValidation": true,
"path": [
    "paths",
    "/auth/login",
    "post",
    "parameters",
    "0"
],
"paramName": "username"
}

但是我已经传递了参数,正如我们在这个 curl 上看到的那样

curl -X POST \
http://localhost:10010/v1/auth/login \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'username=LOGIN&password=MYPASS&userType=employee'

我使用了以下 Swagger.yaml

这是你的定义。

definitions:
UserType: &UserType
 type: string
 enum: [customer, employee, supplier]
 description: >
        User Type:
         * customer.
         * employee.
         * supplier.

这里是使用的路径。

paths: 
 /auth/login:
 # binds a127 app logic to a route
 x-swagger-router-controller: "authentication"
 x-a127-apply: {}
 post:
  summary: Try to login
  description: Try to login with credencials based on username/password. This API will return all "habitant" information if login were successful.
  # used as the method name of the controller
  operationId: login
  consumes: 
    - application/x-www-form-urlencoded
  tags: 
  - Auth
  parameters:
    - name: username
      in: formData
      description: Username to login.
      required: true
      type: string
    - name: password
      in: formData
      description: Password to login.
      required: true
      type: string
      format: password
    - name: userType
      in: formData
      required: true
      default: customer
      <<: *UserType
  responses:
    "200":
      description: Success. If there are attributes with empty values they won´t be returned
      schema:
        # a pointer to a definition
        $ref: "#/definitions/AuthResponse"
    "401":
      description: Unauthorized
      schema:
        $ref: "#/definitions/ErrorResponse"
    "500":
      description: Internal Server Error
      schema:
        $ref: "#/definitions/ErrorResponse"
    "501":
      description: Not Implemented
      schema:
        $ref: "#/definitions/ErrorResponse"

谢谢你的帮助。

标签: httppostswaggerswagger-uiswagger-2.0

解决方案


我认为这可能与您的中间件有关。你用快递吗?

如果是这样,您可以添加类似“app.use(express.urlencoded());”的内容

你应该看看这个链接:

https://www.npmjs.com/package/express-xml-bodyparser


推荐阅读