'x-www-form-urlencoded' ๋˜๋Š” 'form-data' ๐Ÿ˜ต ? 2๋ถ„๋งŒ์— ์„ค๋ช….

6699 ๋‹จ์–ด webdevjavascripthttphtml

TL; DR



ํŒŒ์ผ ์—…๋กœ๋“œ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์—ฌ๊ธฐ์—์„œ form-data๊ฐ€ ์œ ์ผํ•œ ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ทธ๋“ค์€ ๊ฐ™์€ ์ผ์„ ํ•ฉ๋‹ˆ๋‹ค. form-data๋Š” x-www-form-urlencoded๋ณด๋‹ค ๋ฐ์ดํ„ฐ๋ฅผ ๋” ๋ฉ‹์ง€๊ฒŒ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. x-www-form-urlencoded๋Š” .txt ํŒŒ์ผ๋กœ, form-data๋Š” .html ํŒŒ์ผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋ฃจ๊ฐ€ ๋๋‚˜๋ฉด ๋‘˜ ๋‹ค ์ผ๋ถ€ http ํŽ˜์ด๋กœ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜์˜ ๐Ÿ€ getd.io ํ”Œ๋ ˆ์ด๊ทธ๋ผ์šด๋“œ ๋งํฌ ๐Ÿ€๋ฅผ ์‹œ๋„ํ•˜์—ฌ ํ—ค๋”์™€ ๋ณธ๋ฌธ์ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”์ง€ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.
  • Send a request with x-www-form-urlencoded
  • Send a request with form-data

  • ์ปจํ…์ธ  ํƒ€์ž…





    ์ปจํ…์ธ  ํƒ€์ž…


    x-www-form-urlencodedapplication/x-www-form-urlencoded
    ์–‘์‹ ๋ฐ์ดํ„ฐmultipart/form-data; boundary={boundary string}
    form-data์— ๋Œ€ํ•œ ๋น ๋ฅธ ์ฐธ๊ณ  ์‚ฌํ•ญ: ์ผ๋ฐ˜์ ์œผ๋กœ ๋ธŒ๋ผ์šฐ์ €๋Š” ์ž„์˜์˜ {boundary string} , ์˜ˆ๋ฅผ ๋“ค์–ด ----WebKitFormBoundaryKGUmWkAsjo5nUBp2 ๋ฅผ ์ƒ์„ฑํ•˜์ง€๋งŒ ์›ํ•˜๋Š” ๊ฒฝ์šฐ ์ง์ ‘ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋ณด๋ ค๋ฉด ์•„๋ž˜๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

    ํŽ˜์ด๋กœ๋“œ ์š”์ฒญ



    ์•„๋ž˜ ํ•„๋“œ๊ฐ€ ์žˆ๋Š” ๋กœ๊ทธ์ธ ์–‘์‹์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


    ํ•„๋“œ
    ๊ฐ€์น˜


    ์‚ฌ์šฉ์ž ์ด๋ฆ„
    ํ…Œํฌ๋ณด์Šค

    ๋น„๋ฐ€๋ฒˆํ˜ธ
    Pa$$w0๋ฒˆ์งธ


    ์–‘์‹์„ ๊ฒŒ์‹œํ•˜๋ฉด x-www-form-urlencoded์— ๋Œ€ํ•œ ํŽ˜์ด๋กœ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์€ encodeURIComponent() 'd์ž…๋‹ˆ๋‹ค.

    username=techbos&password=Pa%24%24w0rd
    
    form-data ์˜ ๊ฒฝ์šฐ ๊ฐ (ํ‚ค, ๊ฐ’) ์Œ์€ {boundary string}๋ฅผ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ž์ฒด ์„น์…˜์œผ๋กœ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํŒŒ์ผ ์—…๋กœ๋“œ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณด์ด๋Š”์ง€ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ๋งˆ์ง€๋ง‰์— ์ƒ˜ํ”Œ ์„น์…˜๋„ ํฌํ•จํ–ˆ์Šต๋‹ˆ๋‹ค.

    --{boundary string}
    Content-Disposition: form-data; name="username",
    
    techbos
    --{boundary string}
    Content-Disposition: form-data; name="password",
    
    Pa$$w0rd
    --{boundary string}
    Content-Disposition: form-data; name="file"; filename="image.jpg"
    Content-Type: image/jpeg,
    
    --{boundary string}--
    

    ์ธ๋ผ์ธ ์„ค๋ช…:



    ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” API





    x-www-form-urlencoded
    ์–‘์‹ ๋ฐ์ดํ„ฐ


    fetch()
    body as URLSearchParams
    body as FormData

    request()
    form option
    formData option

    axios()
    data as URLSearchParams
    data as FormData



    ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•˜๋Š” API


    x-www-form-urlencoded ์˜ ๊ฒฝ์šฐ bodyParser ์„ ์‚ฌ์šฉํ•˜๋ฉด ํŽ˜์ด๋กœ๋“œ๋ฅผ req.body ์˜ ํ˜•์‹์œผ๋กœ { key, value } ๋กœ ๊ตฌ๋ฌธ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค.

    express.use(bodyParser.urlencoded({ extended: true }));
    express.post('/my-form-endpoint', (req, res) => {
      console.log(req.body.username); // print 'techbos'
    });
    

    Express v4.16.0+์—๋„ ๋™์ผํ•œ ๊ธฐ๋Šฅ์ด ๋‚ด์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
    form-data ๊ตฌ๋ฌธ ๋ถ„์„์„ ์œ„ํ•ด busboy ๋˜๋Š” formidable ๊ณผ ๊ฐ™์€ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฉ๋ฒ•์€ ํ•ด๋‹น ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

    ์–‘์‹ ์ „์†ก/์ฒ˜๋ฆฌ์— ๊ฐ€์žฅ ์ข‹์•„ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ๊ฒฝํ—˜์„ ๊ณต์œ ํ•˜๋ ค๋ฉด ์•„๋ž˜์— ์˜๊ฒฌ์„ ๋‚จ๊ฒจ์ฃผ์„ธ์š” โค๏ธโค๏ธโค๏ธ!

    getd.io์„ ํ™•์ธํ•˜๊ณ  ๋‹ค์Œ์— ๋ณด๊ณ  ์‹ถ์€ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ํ”ผ๋“œ๋ฐฑ์„ ๋‚จ๊ฒจ์ฃผ์„ธ์š” โค๏ธโค๏ธโค๏ธ!

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ