首页 > 解决方案 > 不带正文参数的 POST 请求的 Swagger API 结构

问题描述

没有 body 参数的 POST 请求会给出 JSON 解析错误。提到了下面的错误。

API 调用

api.post('notification/seen')

错误:

{"message":{"name":"SyntaxError","msg":"JSON 中位置 0 的意外标记 n","stack":"SyntaxError: JSON.parse 中位置 0\n 中的意外标记 n ()\n 在 createStrictSyntaxError (/app/node_modules/body-parser/lib/types/json.js:158:10)\n 在解析 (/app/node_modules/body-parser/lib/types/json.js: 83:15)\n 在 /app/node_modules/body-parser/lib/read.js:121:18\n 在 invokeCallback (/app/node_modules/raw-body/index.js:224:16)\n 在完成 (/app/node_modules/raw-body/index.js:213:7)\n 在 IncomingMessage.onEnd (/app/node_modules/raw-body/index.js:273:7)\n 在 IncomingMessage.emit ( events.js:327:22)\n 在 IncomingMessage.EventEmitter.emit (domain.js:486:12)\n 在 endReadableNT (_stream_readable.js:1327:12)\n 在 processTicksAndRejections (internal/process/task_queues.js:80:21)"},"level":"error","timestamp":"2021-09-28T06:29:19.577Z"}

Swagger API 结构

    /notification/seen:
        post:
          tags:
            - Notification
          responses:
            204:
              description: Update Seen Notification Success
            401:
              description: Access Token Missing/Expired
            500:
              description: Something went wrong!

如果我通过更改 Swagger API 结构来添加如下所示的虚拟主体调用相同的 API,我不会收到错误消息。

API 调用

api.post('notification/seen',{id:"data"})

Swagger API 结构

    /notification/seen:
        post:
          tags:
            - Notification
          parameters:
            - in: body
              name: body
              schema:
                type: object
          responses:
            204:
              description: Update Seen Notification Success
            401:
              description: Access Token Missing/Expired
            500:
              description: Something went wrong!

而且,如果我将 POST 更改为 GET 请求,那么我也不会收到错误消息。

如何使用没有正文参数的 POST 请求修复错误?

标签: react-nativeexpressswagger

解决方案


推荐阅读