multipart/form-data

Content-Type

  • HTTP ํ”„๋กœํ† ์ฝœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ๋•Œ, Content-Type ํ—ค๋”๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด์„œ ์—ฌ๋Ÿฌ ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

  • Content-Type์˜ value๋กœ๋Š” MIME ํƒ€์ž… ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ œ๊ณต๋œ๋‹ค. MIME ํƒ€์ž…์˜ ์ข…๋ฅ˜๋Š” Discrete types๊ณผ Multipart types๋กœ ๋ถ„๋ฅ˜๋˜๋ฉฐ, ๊ทธ ์ข…๋ฅ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

    • Discrete Types(๊ฐœ๋ณ„ ํƒ€์ž…)

      text/plain
      text/html
      image/jpeg
      image/png
      audio/mpeg
      audio/ogg
      audio/*
      video/mp4
      application/octet-stream
      โ€ฆ
    • Multipart Types(๋ฉ€ํ‹ฐํŒŒํŠธ ํƒ€์ž…)

      multipart/form-data
      multipart/byteranges
  • ๊ฐœ๋ณ„ ํƒ€์ž…์€ ๋™์ผ ํฌ๋งท์˜ ๋ฐ์ดํ„ฐ๋งŒ์„ ์ „์†กํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋ฉ€ํ‹ฐํŒŒํŠธ ํƒ€์ž…์€ ์—ฌ๋Ÿฌ ํฌ๋งท(plain text, binary ๋“ฑ)์„ ๋™์‹œ์— ์กด์žฌํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

multipart/form-data

  • ๋ฉ€ํ‹ฐํŒŒํŠธ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๋Š” ์š”์ฒญ ์ค‘ Form Data๋ฅผ ์ฃผ๊ณ ๋ฐ›๋Š” ์š”์ฒญ์— ํ•ด๋‹นํ•œ๋‹ค.

  • ์ฃผ๋กœ Form Data๋ฅผ ํ†ตํ•ด text์™€ file์„ ํ•จ๊ป˜ ์ „์†กํ•ด ๋‘˜ ๊ฐ„์˜ ํƒ€์ž…์ด ๋‹ฌ๋ผ ๊ฐœ๋ณ„ ํƒ€์ž…์„ ์ด์šฉํ•  ์ˆ˜ ์—†์„ ๋•Œ ํ•ด๋‹น ํƒ€์ž…์„ ์ด์šฉํ•œ๋‹ค.

    • text๋Š” plain/text, file์€ ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ image/jpg๋‚˜ image/png์™€ ๊ฐ™์€ ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค.

    • ์˜ค์ง plain/text๋งŒ ์ „์†กํ•˜๋Š” Form Data Request์˜ ๊ฒฝ์šฐ application/x-www-form-urlencoded ํƒ€์ž…์„ ์ด์šฉํ•˜๋Š” ํŽธ์ด ๋” ํšจ์œจ์ ์ด๊ณ  ๋ช…์‹œ์ ์ด๋‹ค.

  • ํ•ด๋‹น ํƒ€์ž…์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๋•Œ์—๋Š” ์ „์†ก ๋ฐ์ดํ„ฐ์˜ encoding type๋„ multipart/form-data๋กœ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

    • Content-Type์ด ๋”ฐ๋ฅด๋ฉด ์ธ์ฝ”๋”ฉ ๋กœ์ง๋„ ๋‹ค๋ฅธ๋ฐ, ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์ „์†ก๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์ฝ”๋”ฉํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค.

multipart/form-data ์ „์†ก ๋ฐฉ๋ฒ•

  • Content-Type ํ—ค๋”์— ๋ฐ”์šด๋”๋ฆฌ(boundary)๋กœ ์–ด๋–ค ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ• ์ง€ ์ง€์ •ํ•˜๊ณ , ํ•ด๋‹น ๋ฐ”์šด๋”๋ฆฌ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋ถ„ํ•œ๋‹ค.

  • ๊ฐ ๋ฐ์ดํ„ฐ๋ณ„๋กœ ๊ฐœ๋ณ„ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์•ผ ํ•˜๋ฉฐ ํ•˜๋ฉฐ, ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ ํŒŒ์ผ๋ช…๋„ ๋ช…์‹œํ•ด์•ผ ํ•œ๋‹ค.

  • ์ •๋ฆฌํ•˜์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•์‹์„ ๊ฐ–๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

    Content-Type: multipart/form-data; boundary=boundary๋กœ ์‚ฌ์šฉํ•  ๋ฌธ์ž์—ด
    
    boundary๋กœ ์‚ฌ์šฉํ•  ๋ฌธ์ž์—ด
    Content-Disposition: form-data; name=input name์œผ๋กœ ์ง€์ •๋œ ๋ฌธ์ž์—ด
    
    (๋ฐ์ดํ„ฐ)
    boundary๋กœ ์‚ฌ์šฉํ•  ๋ฌธ์ž์—ด
    Content-Disposition: form-data; name=input name์œผ๋กœ ์ง€์ •๋œ ๋ฌธ์ž์—ด
    
    (๋ฐ์ดํ„ฐ)
    ...

์˜ˆ์‹œ

<form action="http://localhost:8000/" method="post" enctype="multipart/form-data">
  <input type="text" name="myTextField">
  <input type="checkbox" name="myCheckBox">Check</input>
  <input type="file" name="myFile">
  <button>Send the file</button>
</form>

์œ„์™€ ๊ฐ™์€ form data๋ฅผ multipart/form-data๋กœ ์ „์†กํ•œ๋‹ค๋ฉด, ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

...other headers
Content-Type: multipart/form-data; boundary=---------------------------8721656041911415653955004498

-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myTextField"

Test
-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myCheckBox"

on
-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myFile"; filename="test.txt"
Content-Type: text/plain

Simple file.
-----------------------------8721656041911415653955004498--

Reference

Last updated