当我尝试使用Swagger在AngularJS上构建一个post请求时,我收到了错误的请求

时间:2017-01-10 13:42:39

标签: angularjs post swagger

我正在尝试使用Swagger API规范在AngularJS上构建POST请求。

下面是我对Swagger的帖子说明:

"/api/record/": {
  "post": {
    "responses": {
      "201": {
        "description": ""
      }
    },
    "tags": [
      "api"
    ],
    "consumes": [
      "application/json"
    ],
    "operationId": "record_create",
    "parameters": [
      {
        "in": "body",
        "schema": {
          "required": [
            "external_ref",
            "type",
            "code",
            "group"
          ],
          "type": "object",
          "properties": {
            "external_ref": {
              "description": "",
              "type": "string"
            },
            "type": {
              "description": "",
              "type": "string"
            },
            "code": {
              "description": "",
              "type": "string"
            },
            "group": {
              "description": "",
              "type": "string"
            }
          }
        },
        "name": "data"
      }
    ]
  }
}

下面介绍了我的帖子请求的实现:

    return API.record_create({
      body: {
        external_ref: $scope.externalref,
        type: $scope.type.uuid,
        code: $scope.code,
        group: $scope.group
      }
    })
    .then(function(obj) {
      ...
    })
    .catch(function(err) {
      ...
    });

由Swagger为'record_create'函数生成的AngularJS的完整代码如下:

        /**
         * 
         * @method
         * @name #record_create
         * @param {} data - 
         * 
         */
        API.prototype.record_create = function(parameters) {
            if (parameters === undefined) {
                parameters = {};
            }
            var deferred = $q.defer();

            var domain = this.domain;
            var path = '/api/record/';

            var body;
            var queryParameters = {};
            var headers = {};
            var form = {};

            if (this.token.isQuery) {
                queryParameters[this.token.headerOrQueryName] = this.token.value;
            } else if (this.token.headerOrQueryName) {
                headers[this.token.headerOrQueryName] = this.token.value;
            } else {
                headers['Authorization'] = 'Bearer ' + this.token.value;
            }

            headers['Content-Type'] = ['application/json'];

            if (parameters['data'] !== undefined) {
                body = parameters['data'];
            }

            if (parameters.$queryParameters) {
                Object.keys(parameters.$queryParameters)
                    .forEach(function(parameterName) {
                        var parameter = parameters.$queryParameters[parameterName];
                        queryParameters[parameterName] = parameter;
                    });
            }

            this.request('POST', domain + path, parameters, body, headers, queryParameters, form, deferred);

            return deferred.promise;
        };

虽然,我在POST请求中添加了正文的所有必需参数,但是我收到了一个错误的请求(400),响应如下:

{
 "group":["This field is required."],
 "type":["This field is required."],
 "external_ref":["This field is required."],
 "code":["This field is required."]
}

以下显示的帖子请求标题: headers

控制台中唯一的错误如下: console error

您是否了解如何使用此Swagger API规范正确构建POST请求?

1 个答案:

答案 0 :(得分:0)

Swagger自动生成的代码更改了" body"参数到"数据"参数。