首页 > 解决方案 > 如何通过带有 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);

标签: node.jsexpressswagger

解决方案


我解决了这个问题。

this.swagger = new Swagger({
  spec: json,
  usePromise: true,
  requestInterceptor(req) {
    req.headers['Content-Type'] = 'application/json'; // I've add this line.
    return req
  }
});

推荐阅读