react-native - 不带正文参数的 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 请求修复错误?
解决方案
推荐阅读
- sql - 我想对使用 intersect 关键字的多个查询使用 order by
- scala - Scala 警告类型被推断为“AnyVal”
- laravel - 试图在laravel会话foreach中获取非对象的属性“id”
- android - 在列表视图中调用“notifydatasetchanged”时,以前的行正在替换为新行
- css - Flex:如果只有一个 div 可见,则使项目居中对齐
- scala - SBT项目编译问题
- angular - NGXS 状态功能
- android - 我如何在 android studio 离线模式下使用 Firebase crashlytics
- javascript - Javascript:使用过滤器复制数组
- sql - 从 Varchar 中删除起始字符