๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”ฅ ํ”„๋กœ์ ํŠธ/๋ผ๊พธ๋ผ๊พธ

[์„ค๊ณ„] Image ์ „์†ก๊ณผ ๋ฐ์ดํ„ฐ ์ „์†ก API ๋ถ„๋ฆฌ

by nitronium102 2023. 8. 13.

ํ˜„์žฌ ์ƒํ™ฉ

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ๋ฐ์ดํ„ฐ JSON๊ณผ ์ด๋ฏธ์ง€(form-data)๋ฅผ ํ•˜๋‚˜์˜ API๋ฅผ ํ†ตํ•ด ์ „์†กํ•ฉ๋‹ˆ๋‹ค.

 

๋ฌธ์ œ์ 

๋ณดํ†ต ์ด๋ฏธ์ง€์™€ JSON๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ๋ณด๋‚ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. API๋ฅผ ์ „์†กํ•  ๋•Œ๋Š” ๋ณดํ†ต content/type์„ application/json์œผ๋กœ ์ „์†กํ•˜๋Š”๋ฐ ์ด๊ฒƒ์€ multipart/formdata ๋ฐฉ์‹์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

์ด์— ๋”ฐ๋ผ, ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€ ์ธ๋ฐ

  1. ๋ถ„๋ฆฌํ•ด์„œ ์š”์ฒญํ•˜๊ธฐ
    ์„œ๋ฒ„๋Š” ์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•˜๊ณ , ์ €์žฅ๋œ ์ด๋ฏธ์ง€์˜ id(๋˜๋Š” ํŒŒ์ผ๋ช…)๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
    ํด๋ผ์ด์–ธํŠธ๋Š” ์ด๋ฏธ์ง€์˜ id์™€ ์ „์†กํ•  ๋ฐ์ดํ„ฐ๋ฅผ application/json์œผ๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
  2. base64 ์ธ์ฝ”๋”ฉ
    ์ด๋ฏธ์ง€๋ฅผ base64๋กœ ์ธ์ฝ”๋”ฉํ•ด์„œ application/json์œผ๋กœ ์ „์†กํ•  ๋ฐ์ดํ„ฐ์™€ base64๋กœ ์ธ์ฝ”๋”ฉ๋œ ์ด๋ฏธ์ง€๋ฅผ ํ•จ๊ป˜ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.


2๋ฒˆ ๋ฐฉ๋ฒ•์€ ๋””๋น„์— base64 ๋กœ ์ €์žฅํ• ๊ฒฝ์šฐ, ์ฐจ์ง€ํ•˜๋Š” ์šฉ๋Ÿ‰์ด ๋งŽ์ด ๋Š˜๊ณ , ํŠธ๋ž˜ํ”ฝ๋„ ์ฆ๊ฐ€ํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

 

๋”ฐ๋ผ์„œ ๊ฐœ์ธ์ ์œผ๋กœ๋Š” 1๋ฒˆ ๋ฐฉ๋ฒ•์ด ๋” ์ ํ•ฉํ•ด๋ณด์ธ๋‹ค.

๋Œ“๊ธ€