首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >open api 3发布一组文件

open api 3发布一组文件
EN

Stack Overflow用户
提问于 2018-03-23 00:37:32
回答 2查看 3.2K关注 0票数 2

我正在使用swagger hub到create this API;但是它不支持UI中的多个文件,所以我不确定我这样做是否正确

我的目标是实现以下目标

代码语言:javascript
复制
item:{Json describe the item}
images[] = images for item posted as an array
titles[] = Parallel array to images that has the title for image
alt_texts[] = Parallel array to images that has the alt text for image

这必须是多部分的,因为它是文件;但我不确定我是否正确设置了结构。

Swagger/Open API代码

代码语言:javascript
复制
post:
  summary: Add a new item to the store
  description: ''
  operationId: addItem
  requestBody:
    content:
      multipart/form-data:
        schema:
          $ref: '#/components/schemas/NewItemWithImage'
    description: Item object that needs to be added to the store
    required: true


NewItemWithImage:
  type: object
  properties:
    item:
      $ref: '#/components/schemas/NewItem'
    images[]:
      type: array
      items:
        type: string
        format: binary
    titles[]:
      type: array
      items:
        type: string
    alt_texts[]:
      type: array
      items:
        type: string
    variation_ids[]:
      type: array
      items:
        type: string
  required:
    - item
EN

回答 2

Stack Overflow用户

发布于 2018-03-31 16:04:23

根据OpenAPI 3规范中的File Upload部分:

文件上传

文件使用带有format: binaryformat: base64type: string架构,具体取决于文件内容的编码方式。

多文件上传

使用multipart媒体类型来定义上传任意数量的文件(文件数组):

代码语言:javascript
复制
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                filename:
                  type: array
                  items:
                    type: string
                    format: binary

您当前的定义完全符合规范:

代码语言:javascript
复制
requestBody:
  content:
    application/json:
      schema:
        $ref: '#/components/schemas/NewItemWithImageUrl'
    multipart/form-data:
      schema:
        $ref: '#/components/schemas/NewItemWithImage'

NewItemWithImage:
  type: object
  properties:
    item:
      $ref: '#/components/schemas/NewItem'
    images[]:
      type: array
      items:
        type: string
        format: binary
    titles[]:
      type: array
      items:
        type: string
    ...
票数 4
EN

Stack Overflow用户

发布于 2019-01-29 06:37:48

到目前为止,swagger-ui的代码在curlify.js中失败了

代码语言:javascript
复制
        if (v instanceof win.File) {
          curlified.push( `"${k}=@${v.name}${v.type ? `;type=${v.type}` : ""}"` )
        } else {
          curlified.push( `"${k}=${v}"` )
        }

curlify.js未将阵列考虑在内,正在发送:

curl -X POST "http://localhost:9122/upload-all" -H "accept: */*" -H "Content-Type: multipart/form-data" -F "my-attachment=[object File],[object File]"

而不是像这样的东西:

curl -X POST "https://foo/v1/api/upload/" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "myfile=@bla;type=application/zip" -F "myfile=@foo;type=application/zip"

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49433984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档