node.js - 如何通过带有 swagger-client 的 put 方法发送请求正文参数
问题描述
我正在使用招摇客户端。这是客户端的 npm 库模块。但我有一个问题。在服务器端,我使用的是 express。
即使我尝试使用 put 方法发送 body 参数,express 也不接受参数。只能访问路径参数或查询参数。这些都没有问题。
这是代码和招摇的json。
■ json 格式的请求路径。
"/test/{hogehoge}/sample": {
"put": {
"summary": "summary",
"description": "description",
"tags": [
"Sample"
],
"operationId": "operationId",
"parameters": [
{
"name": "path",
"in": "path",
"description": "path description",
"required": true,
"type": "integer"
},
{
"name": "body",
"in": "body",
"description": "body",
"required": true,
"schema": {
"$ref": "#/definitions/bodySchema"
}
}
],
■ 请求路径模式以将方法放在上面。
"bodySchema": {
"type": "object",
"properties": {
"bodySchemaVersion": {
"type": "string",
"description": "bodySchema version"
}
}
},
■ 快递日志
parameter:
{ query: {},
path:
{ param1: 'param1',
param2: 'param2' },
body: {} } } ← body that it should be bodySchema param.
■客户端vue
import Swagger from 'swagger-client';
import swaggerJson from '@/assets/swagger/swagger.json';
....
// opts include
// { bodySchema: { bodySchemaVersion: 'string text' } }
await this.swagger.client.apis[tagName][funcName](_opts);
解决方案
我解决了这个问题。
this.swagger = new Swagger({
spec: json,
usePromise: true,
requestInterceptor(req) {
req.headers['Content-Type'] = 'application/json'; // I've add this line.
return req
}
});
推荐阅读
- c# - IdentityServer4 和单独的用户身份验证 API
- nginx - 为什么 Nginx 用这 3 个素数实现 ip_hash 负载均衡方法
- azure - 我的世博分离应用程序构建在 Visual Studio 应用程序中心失败
- html - IE11 中的 HTML 选择菜单问题
- react-native - 无法访问 axios 请求的已加载和正在进行的对象总数事件
- laravel - 工厂中的 Laravel 伪造文件不起作用
- c# - 无法使用 KeyFilterAttribute 解决 AutoFac 键控服务不起作用
- java - HibernateQuery 返回对象列表而不是实体
- r - replace() 中的奇怪行为
- jquery - 使用 Bootstrap 和 jQuery 实现 select2 时,新的文本字段重叠