HTTP는 웹 서비스 개발자라면 당연히 알고 있어야할 핵심 프로토콜이다.
HTTP통신은 어떻게 구성되고 사용되어야 하는지 알아보자.
GET
, POST
, PUT
, DELETE
, OPTIONS
등이 있다.GET
과 POST
가 쓰임./login
)1.0
, 1.1
, 2.0
등)GET/search HTTP/1.1
:
이 사용됨)Key: Value
Host: google.com
=> Key = HOST
, Value = google.com
general headers
, request
, entity headers
) 너무 자세한 부분임으로 3부분으로 구성되어 있다는 것만 알고 있어도 괜찮다.application/json
)Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json
Content-Length: 257
Host: google.com
User-Agent: HTTPie/0.9.3
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"
}
HTTP Response 구조
200
HTTP/1.1 404 Not Found
Headers
User-Agent
대신에 Server
헤더가 사용된다.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
uri에서 어떤 method를 요청 가능한가(GET
? POST
?)를 알고 싶으면 먼저 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
와 비슷하다. 데이터를 생성 할때 사용되는 Method.POST
와 겹치기 때문에 PUT
을 사용하는 곳도 있고 POST
로 통일해서 사용하는 곳도 있는데, 최근 몇년 사이에 POST
에 밀려서 잘 사용안되는 추세.PUT
과 마찬가지로 POST
에 밀려서 잘 사용안되는 추세.HTTP/1.1 301 Moved Permanently
Location: http://www.example.org/index.asp
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