request-line
header (General|Request|Entity) fields + CRLF (없거나 1개 이상)
empty line: header와 body를 구분하기 위해 사용
message-body (optional)
✔ CRLF는 줄바꿈을 의미하는 문자로 CR(Carriage Return, \r)과 LF(Line Feed, \n)이 조합된 것이다.
GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
예시 2) message-body가 있음
POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: application/x-www-form-urlencoded
Content-Length:length
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
licenseID=string&content=string&/paramsXML=string
GET /hello.htm HTTP/1.1
위와 같이 HTTP-METHOD URI HTTP-PROTOCOL
의 형태로 작성한다.
GET: HTTP method가 GET임을 의미
/hello.htm: 읽어들일 resource. 정확히는 URI/hello.htm 위치에 있는 resource를 요청
HTTP/1.1: 사용할 HTTP 프로토콜. HTTP/1.1이 가장 흔히 사용되지만, 좀 더 최신 버전으로는 HTTP/2.0이 있다.
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Header에는 요청에 대한 metadata가 들어간다. 대소문자를 구분하지 않는 이름과 콜론 ‘:’ 다음에 오는 값(줄 바꿈 X)으로 이루어져 있다.
각 헤더에 대한 설명은 이 링크에 자세히 나와있다. 그 중 일부만 설명하자면,
인증 관련
캐싱
연결관리
컨텐츠 협상
message body 정보
Content-Type: text/html; charset=utf-8
Content-Type: multipart/form-data; boundary=something
media-type: 리소스 혹은 데이터의 MIME type. 위 예시에서는 text/html
, multipart/form-data
부분.
기본값은 보통 application/octet-stream
이다.
요청 컨텍스트
이 외에도 쿠키, CORS, 보안 등에 관련된 다양한 HTTP 헤더가 있다.
Body 부분에는 사실상 무엇이든 들어갈 수 있다! 다만 데이터에 따라 header의 message body 정보가 달라질 것이다.
POST /api/authors HTTP/1.1
Host: myWebApi.com
Content-Type: application/json
Cache-Control: no-cache
{
"Name": "Felipe Gavilán",
"Age": 999
}
HTTP Response는 다음과 같은 구조를 갖는다.
status-line
header + CRLF (없거나 1개 이상)
empty line: header와 body를 구분하기 위해 사용
message-body (optional)
예시)
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
Content-Length: 88
Content-Type: text/html
Connection: Closed
Status-line 부분을 제외하면 request의 구조와 동일하다.
HTTP-Version Status-Code Reason-Phrase CRLF
Reason-phrase는 status code를 간략하게 텍스트로 설명해주는 부분이다.참고 자료
HTTP - Requests
Anatomy of an HTTP request
Accept-Language - HTTP | MDN
[HTTP] keep alive란? (persistent connection에 대하여)