Swagger 2.0将文件上传到SP Online,但不会发送额外的内容

问题描述 投票:0回答:1

我希望有人可以在这里指出我的错误。我在swaggerhub上使用了以下swagger定义,它将通过其余的api将文件上传到Sharepoint文档库

{
  "swagger" : "2.0",
  "info" : {
    "description" : "defaultDescription",
    "version" : "2",
    "title" : "defaultTitle"
  },
  "host" : "someSite.sharepoint.com",
  "schemes" : [ "https" ],
  "paths" : {
    "/sites/ms/_api/Web/GetFolderByServerRelativeUrl('doc/test/tt')/Files/Add(url='{filename}',overwrite=true)" : {
      "post" : {
        "consumes" : [ "multipart/form-data" ],
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "in" : "formData",
          "name" : "upfile",
          "type" : "file",
          "required" : true,
          "description" : "The file to upload."
        },
        {             
          "in" : "path",
          "name" : "filename",
          "type" : "string",
          "required" : true
        } ],
        "responses" : {
          "200" : {
            "schema" : {
              "type" : "string"
            },
            "description" : "Definition generated from Swagger Inspector"
          }
        }
      }
    }
  }
}

问题是我无法打开SP上的任何文件,因为它们已经坏了,我相信我找到了用txt文件测试的原因。

当我在SP doc库上打开它时,我将发送一个仅包含Sample text bu的文本文件,它包含以下所有内容

-------------------------------28947758029299
Content-Disposition: form-data; name="upfile"; filename="myt.txt"
Content-Type: text/plain

Sample text
-------------------------------28947758029299--

是我的内容类型的问题,还是我应该以不同的方式使用参数,我尝试研究这个,但我找到的只是匹配原始的guid我发现https://swagger.io/docs/specification/2-0/file-upload/

rest swagger-2.0
1个回答
0
投票

我们在项目中遇到了类似的问题。根本原因是Swagger 2.0不允许您为类型文件指定不同的Content-Type。您必须使用multipart / form-data。详情请见:https://swagger.io/docs/specification/2-0/file-upload/

要解决此问题,您必须将类型从文件更改为通用对象。例如:

  paths:
  /sharepoint_upload/:
    post:
      description: "This will uploads a document to SharePoint."
      operationId: "uploadDocuments"
      consumes:
      - "application/octet-stream"
      produces:
      - "application/json"
      parameters:
      - name: "documentBody"
        in: "body"
        description: "The actual document"
        required: true
        schema:
          type: "object"
      responses:
        200:
          description: "OK - Your request was successfully completed."
        400:
© www.soinside.com 2019 - 2024. All rights reserved.