본문 바로가기
개발의 정석/웹

[#http] multipart/form raw 포맷 서버 업로드 구현

by 발자개발 2020. 3. 24.

 

 

 

여러 파일을 한번에 업로드할 때 헤더 Content-Type: multipart/form-dataPOST 요청을 하게 된다. 이때 request body에는 어떤 형식으로 데이터가 들어가는지 알아보자

 

예시로

test.txt

this is first file.
baljagae tistory

 

test2.txt

this is second file.
Hello World:)

 

위 두 파일을 Content-Type: multipart/form-data 으로 보내면 request body에 아래와 같은 포맷으로 서버에 넘어가게 된다. \r\n으로 해당하는 파일의 헤더를 구분하고, 이 헤더에는 파일명, 타입 등이 기재되어 있다. 그리고 \r\n\r\n 다음에 직접 파일의 내용을 담고 있다. 서버에서는 \r\n\r\n 뒤부터 그 다음 나오는 ----boundary 전까지의 데이터를 파일로 쓰면 업로드를 구현할 수 있다.

 

POST / HTTP/1.1[\r][\n]
Host: 127.0.0.1:10099[\r][\n]
User-Agent: curl/7.54.0[\r][\n]
Accept: */*[\r][\n]
Content-Length: 410[\r][\n]
Expect: 100-continue[\r][\n]
Content-Type: multipart/form-data; boundary=------------------------4b656e53c3013d8c[\r][\n]
[\r][\n]
--------------------------4b656e53c3013d8c[\r][\n]
Content-Disposition: form-data; name="data/test"; filename="test.txt"[\r][\n]
Content-Type: text/plain[\r][\n]
[\r][\n]
this is first file.
baljagae tistory
[\r][\n]
--------------------------4b656e53c3013d8c[\r][\n]
Content-Disposition: form-data; name="data/test2"; filename="test2.txt"[\r][\n]
Content-Type: text/plain[\r][\n]
[\r][\n]
this is second file.
Hello World:)
[\r][\n]
--------------------------4b656e53c3013d8c--[\r][\n]

 

 

 

 

댓글