使用javascript swagger客户端上传文件时遇到问题,并做出反应

时间:2019-03-11 08:43:31

标签: javascript reactjs swagger swagger-codegen react-dropzone

我对Swagger代码生成的源代码有疑问。 我想上传一个带有react的文件。为此,我创建了一个Dropzone并获取文件的路径。如果我按文档中的原样使用生成的客户端,它将无法正常工作。不幸的是,该文件未发送。仅文件名。调试控制台也不会显示二进制数据已发送。

请求未正确执行。该文件将不会上传。参数“ file”只是文件名,而不是二进制数据。

Swagger-codegen版本

openapi-generator-cli-3.3.4.jar

Swagger声明文件内容

Swagger .yaml:

  /orders/upload:
    post:
      operationId: "orderUploadPart"
      summary: "upload a textual or audio part of an order"
      tags:
        - "orders"
      description: "This funktion uploads a textual or audio part of an order to the sense.ai.tion cloud system. 
      The result is the resource identifier, that must be used in the order request."
      consumes:
        - multipart/form-data
      parameters:
        - in: "formData"
          name: "file"
          type: "file"
          required: true
          description: "the file to upload"
        - in: "formData"
          name: "media"
          type: "string"
          enum:
            - "text"
            - "wav"
            - "mp3"
          required: true
          description: "the media type of the the upload, can be ***text***, ***wav*** or ***mp3***"

代码:

var apiInstance = new SenseaitionApi.OrdersApi();
var file = "/path/to/file"; // File | the file to upload
var media = "media_example"; // String | the media type of the the upload, can be ***text***, ***wav*** or ***mp3***
var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully. Returned data: ' + data);
  }
};
apiInstance.orderUploadPart(file, media, callback);

就像在https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/javascript/docs/PetApi.md#uploadFile

Screenshot Chrome DevTools

用于生成的命令行

java -jar ${GOPATH}/bin/openapi-generator-cli.jar generate -i service_js_api.yaml -g javascript -o clients/javascript/senseaition-api-js -Dio.swagger.parser.util.RemoteUrl.trustAll=true

1 个答案:

答案 0 :(得分:0)

我发现了错误。生成的Javascript代码的文档是错误的。上传文件(Javascript对象)时,必须传递路径,而不是路径。

此行是错误的:

var file = "/path/to/file"; // File | the file to upload