How to post files in Swagger (OpenAPI)?

后端 未结 4 876
情书的邮戳
情书的邮戳 2020-12-13 23:08

I am using Swagger to document my REST services. One of my services requires a CSV file to be uploaded. I added the following to the parameters section in my JS

相关标签:
4条回答
  • 2020-12-13 23:42

    Mine seems to work with

     "paramType": "formData",
     "dataType": "file",
    
    0 讨论(0)
  • 2020-12-13 23:46

    OpenAPI Specification 2.0

    In Swagger 2.0 (OpenAPI Specification 2.0), use a form parameter (in: formData) with the type set to file. Additionally, the operation's consumes must be multipart/form-data.

      consumes:
        - multipart/form-data
      parameters:
        - name: file
          in: formData   # <-----
          description: The uploaded file data
          required: true
          type: file     # <-----
    

    OpenAPI Specification 3.0

    In OpenAPI Specification 3.0, files are defined as binary strings, that is, type: string + format: binary (or format: byte, depending on the use case). File input/output content is described with the same semantics as any other schema type (unlike OpenAPI 2.0):

    Multi-part request, single file:

    requestBody:
      content:
        multipart/form-data:
          schema:
            type: object
            properties:
              # 'file' will be the field name in this multipart request
              file:
                type: string
                format: binary
    

    Multi-part request, array of files (supported in Swagger UI 3.26.0+ and Swagger Editor 3.10.0+):

    requestBody:
      content:
        multipart/form-data:
          schema:
            type: object
            properties:
              # The property name 'file' will be used for all files.
              file:
                type: array
                items:
                  type: string
                  format: binary
    

    POST/PUT file directly (the request body is the file contents):

    requestBody:
      content:
        application/octet-stream:
          # any media type is accepted, functionally equivalent to `*/*`
          schema:
            # a binary file of any type
            type: string
            format: binary
    

    Note: the semantics are the same as other OpenAPI 3.0 schema types:

    # content transferred in binary (octet-stream):
    schema:
      type: string
      format: binary
    

    Further information:

    • Considerations for File Uploads
    • Special Considerations for multipart Content
    • File Upload and Multipart Requests
    0 讨论(0)
  • 2020-12-13 23:49

    finally i found answer for this, actually previously there is no support for file upload, now they updated swagger-ui.js file. You need to replace your old one with new and also you have to define these properties under Parameters for particular parameter:

     "paramType": "body",
     "dataType": "file",
    
    0 讨论(0)
  • 2020-12-14 00:00

    I am using Open API v 3.0.3

    Here's what my swagger.json looks like:

    "/media/upload": {
          "post": {
            "tags": ["Media"],
            "name": "Upload Media",
            "description": "Uploads a Media file to the server.",
            "requestBody": {
              "required": true,
              "content": {
                "multipart/form-data": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "media": {
                        "type": "string",
                        "format": "base64"
                      }
                    }
                  }
                }
              }
            }
          }
        }
    

    Here's how it shows up in swagger:

    0 讨论(0)
提交回复
热议问题