如何通过带有 swagger-client 的放置方法发送请求正文参数



我正在使用swagger-client。 这是一个用于客户端的 npm 库模块。 但我有一个问题。 在服务器端,我正在使用快递。

即使我尝试使用 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
}
});

最新更新