July 27, 2020
하이퍼텍스트 문서 (HTML)를 교환하기 위해 만들어진 Protocol (통신 규약)
HTTP 기본적으로 요청/응답 (request/response) 구조로 되어 있다.
HTTP는 Stateless이다.
HTTP request 메시지는 크게 3부분으로 구성된다.
Start LIne
Start Line 또한 3부분으로 구성되어 있다.
HTTP Method
GET, POST, PUT, DELETE, OPTIONS 등등이 있다.GET과 POST가 쓰인다.Request target
/loginHTTP Version
GET /search HTTP/1.1Headers
request에 대한 추가 정보(addtional information)를 담고 있는 부분.
key-Value 값으로 되어있다. (: 이 사용됨)
key:valueHOST: [google.com](http://google.com) ⇒ Key = HOST, Value = google.comgeneral headers, request headers, entity headers)자주 사용되는 header
Host
User-Agent
Accept
Connection
Content-Type
application/jsonContent-Length
메시지 body의 길이
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json
Content-Length: 257
Host: google.com
User-Agent: HTTPie/0.9.3Body
Body가 없는 request도 많다.
예를 들어, GET request들은 대부분 body가 없는 경우가 많음
POST /payment-sync HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 83
Content-Type: application/json
Host: intropython.com
User-Agent: HTTPie/0.9.3
{
"imp_uid": "imp_1234567890",
"merchant_uid": "order_id_8237352",
"status": "paid"
}Response도 request와 마찬가지로 크게 3부분으로 구성되어 있다.
Status Line
3부분으로 구성되어 있다.
Status code : 응답 상태를 나타내는 코드, 숫자로 이루어져 있다.
200Status text : 응답 상태를 간략하게 설명해주는 부분.
HTTP/1.1 404 Not FoundHeaders
다만, response에서만 사용되는 header 값들이 있다.
User-Agent대신 Server 헤더가 사용된다.Body
HTTP/1.1 404 Not Found
Connection: close
Content-Length: 1573
Content-Type: text/html; charset=UTF-8
Date: Mon, 20 Aug 2018 07:59:05 GMT
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 404 (Not Found)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>404.</b> <ins>That’s an error.</ins>
<p>The requested URL <code>/payment-sync</code> was not found on this server. <ins>That’s all we know.</ins>예를 들어, /update url에서 어떤 메소드를 요청 가능한지 알고 싶으면 먼저 OPTIONS요청을 사용해서 확인하게 된다.
http -v OPTIONS http://example.org
OPTIONS / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 0
Host: example.org
User-Agent: HTTPie/0.9.3
HTTP/1.1 200 OK
Allow: OPTIONS, GET, HEAD, POST
Cache-Control: max-age=604800
Content-Length: 0
Date: Mon, 20 Aug 2018 08:37:45 GMT
Expires: Mon, 27 Aug 2018 08:37:45 GMT
Server: EOS (vny006/0450)POST와 비슷하다. 데이터를 생성 할 때 사용되는 MethodPOST와 겹치기 때문에 PUT을 사용하는 곳도 있고 POST로 통일해서 사용하는 곳도 있는데, 최근 몇년 사이에 **POST에 밀려서 잘 사용 안되는 추세.**해당 URI가 다른 주소로 바뀌었을때 보내는 코드
HTTP/1.1 301 Moved Permanently
Location: http://www.example.org/index.asp주로 요청에 포함된 input값들이 잘못된 값들로 보내졌을때 사용되는 코드
http -v google.com/no-such-uri
GET /no-such-uri HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: google.com
User-Agent: HTTPie/0.9.3
HTTP/1.1 404 Not Found
Content-Length: 1572
Content-Type: text/html; charset=UTF-8
Date: Mon, 20 Aug 2018 08:46:48 GMT
Referrer-Policy: no-referrer/login, /newshttps://finance.naver.com/marketindex/POST, GET웹상에서 사용되는 여러 리소스를 HTTP URI로 표현하고 그 리소스에 대한 행위를 HTTP Method로 정의하는 방식.
GET과 POST만 사용한다.PUT과 DELETE등도 사용하는 곳도 있지만, 그냥 GET과 POST만 사용하는 것이 단순하기 때문에 GET과 POST만 사용하는 추세.예를 들어, 삼성전자 주식 정보를 받기 위한 HTTP 요청:
HTTP GET [https://api.trueshort.com/stock/005930](https://api.trueshort.com/stock/005930)유저의 보유 주식 종목들을 DB에 저장하는 HTTP 요청:
HTTP POST https://api.trueshort.com/user/portfolio
{
"user_id" : 1,
"stocks": [
"005930",
"298730",
"378900"
]
}self-descriptivenessRESTful API는 그 자체 만으로도 API의 목적이 쉽게 이해가 된다.
HTTP GET [https://api.trueshort.com/stock/005930](https://api.trueshort.com/stock/005930) 요청의 경우, 문서나 주석이 없이도 ”https://api.trueshort.com 라는 API에서 삼성전자 주식에 관한 정보를 HTTP 요청을 통해 받아오는 구나” 라는 해석이 쉽게 가능하다./(슬래시)는 계층 관계를 나타낼때 사용된다.
https://api.trueshort.com/stock/005930이라는 구조라면, KOSPI에 속해있는 주식(Stock)중 삼성전자(005930)dmf skxksosms rjtdlek.https://api.shopping.com/books/novel/stephenking 이라는 구조 이라면, 책들 중 소설 그리고 소설 중 Stephen King의 소설을 나타내는 구조이다._(Underscore)는 주로 포함하지 않고 또한 영어 대문자보다 소문자를 쓴다. 그리고 너무 긴 단어는 잘 사용하지 않는다. 이 모든건 가독성을 높이기 위해서다.URI는 명사를 사용한다.
/books/novel/stephenking 이라고 하지 /books/novel/get-stephenking 이라고 잘 하지 않는다.GET, POST 같은 HTTP Method를 통해 표현하기 때문이다.