File Upload

정민교·2023년 7월 11일
0

📒File Upload

OpenAPI 3.0에서는 request content로 업로드 되는 파일과 multipart 요청으로 업로드 되는 파일을 정의할 수 있다.

request payload가 file을 포함한다는 것을 설명하기 위해 requestBody 키워드를 사용한다.

content 밑에 request media type을 명시한다.

파일은 type: string format: binary or base64를 schema에 명시한다.

requestBody:
  content:
    image/png:
      schema:
        type: string
        format: binary

위 definition은 아래 HTTP request와 상응하다.

POST /upload
Host: example.com
Content-Length: 808
Content-Type: image/png
[file content goes there]

✔️Upload via Multipart Requests

파일이 다른 데이터와 함게 전송된다면 multipart media type을 사용한다.

      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                orderId:
                  type: integer
                userId:
                  type: integer
                fileName:
                  type: string
                  format: binary

위와 상응하는 HTTP request payload는 mutiple parts를 포함한다.

POST /upload
Host: example.com
Content-Length: 2740
Content-Type: multipart/form-data; boundary=abcde12345
--abcde12345
Content-Disposition: form-data; name="orderId"
1195
--abcde12345
Content-Disposition: form-data; name="userId"
545
--abcde12345
Content-Disposition: form-data; name="fileName"; filename="attachment.txt"
Content-Type: text/plain
[file content goes there]
--abcde12345--

✔️Multiple File Upload

multipart media type을 사용해서 파일의 배열 업로드를 정의할 수 있다

      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                filename:
                  type: array
                  items:
                    type: string
                    format: binary
POST /upload
Host: example.com
Content-Length: 2740
Content-Type: multipart/form-data; boundary=abcde12345
--abcde12345
Content-Disposition: form-data; name="fileName"; filename="file1.txt"
Content-Type: text/plain
[file content goes there]
--abcde12345
Content-Disposition: form-data; name="fileName"; filename="file2.png"
Content-Type: image/png
[file content goes there]
------WebKitFormBoundaryWfPNVh4wuWBlyEyQ
Content-Disposition: form-data; name="fileName"; filename="file3.jpg"
Content-Type: image/jpeg
[file content goes there]
--abcde12345--
profile
백엔드 개발자

0개의 댓글