Swagger 无法识别 x-www-urleconded 参数

Swagger doesn't recognize x-www-urleconded parameters

我有一个关于 2.0 版的 swagger 文档 我把路径消耗为 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"

感谢您的帮助。

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

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

你应该看看这个link:

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