【问题标题】:missing header "content-type" on POST request swagger node.jsPOST 请求 swagger node.js 上缺少标头“内容类型”
【发布时间】:2021-07-30 06:31:31
【问题描述】:

Swagger中发送带有正文的POST请求,请求中没有任何内容...我认为问题是因为它没有标头“内容类型"

postman中测试同一个请求正常到达(为什么有header)。

当我在 swaggerOptions 中添加授权时停止发送标头,特别是“openapi”、“schemes”和“security

swaggerOptions.json

const swaggerOptions = {
    definition: {  
        openapi: '3.0.0',  
        info: {            
            version: "1.0.0",
            title: "Diniz API",
            description: "Rest API Diniz",
            servers: ['http://localhost:5000']
        },
        components: {
            securitySchemes: {
                bearerAuth: {
                    type: 'apiKey',
                    name: 'Authorization',
                    scheme: 'bearer',
                    in: 'header',
                },
            }
        },
        security: [{ 
            bearerAuth: [], 
        }],
    },
    apis: ['routes/*.js']
}

【问题讨论】:

  • 请贴出/signup端点的注解。从屏幕截图来看,端点似乎使用 OpenAPI 2.0 关键字(例如 in: body 参数)进行了注释,而它应该使用 OpenAPI 3.0 关键字(即 requestBody)来匹配您在 swaggerOptions 中指定的 OpenAPI 版本。
  • 这是我来自 /signup i.imgur.com/HupRIp6.png 的注释我做错了什么?试图阅读文档但不理解

标签: node.js http-headers swagger swagger-ui


【解决方案1】:

在您的 /signup 注释中,将 parameters 部分替换为以下 requestBody 部分:

*      ...
*      summary: Create new account
*
*      requestBody:
*        required: true
*        content:
*          application/json:
*            schema:
*              type: object
*              required:
*                - name
*                - email
*                - ...   # add the rest of the required properties
*              properties:
*                name:
*                  type: string
*                ...     # add the rest of the properties

【讨论】:

    猜你喜欢
    • 2015-02-28
    • 2013-09-09
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 2017-01-01
    • 2020-11-12
    • 1970-01-01
    相关资源
    最近更新 更多