Swagger 2.0将文件上传到SP Online,该文件不会通过

时间:2018-11-19 14:09:52

标签: rest swagger-2.0

我希望有人可以在这里指出我的错误。 我在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文档库中打开时,我将发送仅包含Sample text bu的文本文件,

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

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

是我的内容类型出现问题还是应该以其他方式使用参数,我尝试对此进行研究,但发现与我找到的原始GUI匹配 https://swagger.io/docs/specification/2-0/file-upload/

1 个答案:

答案 0 :(得分: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: