Programming/Python

깔끔한 파이썬 탄탄한 백엔드 2. HTTP 구조와 이해 - 비제이퍼블릭

armyost 2021. 5. 2. 22:33
728x90

HTTP는 웹상에서 서로 다른 서버 간에 하이퍼텍스트 문서, 즉 HTML을 서로 주고받을 수 있도록 만들어진 프로토콜, 통신 규약이다. 웹상에서 네트워크를 통해 서버 사이에 통신할 때 어떠한 형식으로 서로 통신하자고 규정해 놓은 "통신 형식" 혹은 "통신 구조"라고 보면 된다. 

 

HTTP 통신 방식에는 2가지 특징이 있다. 하나는 HTTP의 요청과 응답 방식이고 또 다른 특징은 stateless다. 

 

HTTP 요청과 응답

먼저 HTTP 프로토콜에 대해서 이해해야 할 것은, HTTP는 기본적으로 요청과 응답의 구조로 되어 있다는 것이다. HTTP를 기반으로 통신할때 클라이언트가 먼저 HTTP 요청을 서버에 보내면 서버는 요청을 처리한 후 결과에 따른 HTTP응답을 클라이언트에게 보냄으로써 하나의 HTTP 통신이 된다. 

 

그러므로 백엔드 API시스템의 엔드포인트 구현도 기본적으로 HTTP 요청을 인풋으로 받아서 HTTP 응답을 아웃풋으로 리턴하는 구조로 구현을 하게 된다. 

 

Stateless

두번째로 HTTP프로토콜에 대해서 이해해야 하는 것은, 바로 HTTP 통신은 stateless라는 점이다. stateless는 말 그대로 상태가 없다는 뜻으로, HTTP통신에서는 상태라는 개념이 존재하지 않는다. 앞에서 HTTP통신은 요청과 응답의 구조로 되어 있으며, 서버가 클라이언트로 부터 요청을 받고 응답을 보내는 것이 하나의 HTTP 통신이라고 언급했다. 클라이언트와 서버는 HTTP통신을 여러번 주고받는 것이 일반적인데, HTTP프로토콜에서는 동일한 클라이언트와 서버가 주고받은 HTTP통신들이라도 서로 연결되어 있지 않다. 즉 각각의 HTTP통신은 독립적이며 그전에 처리된 HTTP 통신에 대해서 전혀 알지 못한다. 

 

HTTP 요청 구조

1)Start Line

GET /search HTTP/1.1

HTTP Method
해당 request가 의도한 action을 정의하는 부분. 예를들어 서버로부터 어떠한 데이터를 받고자 한다면 GET요청을 보내고, 서버에 새로운 데이터를 저장하고자 한다면 POST요청을 보내는 등의 식이다. HTTP 메소드에서는 GET, POST, PUT, DELETE, OPTIONS 등 여러 메소드들이 있다. 


Request target
해당 request가 전송되는 목표 주소를 말한다. URI를 말한다. 


HTTP Version
HTTP 버전을 말한다.

 

 

2)헤더

Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json
Content-Length: 257
Host: google.com
User-Agent: HTTPie/0.9.3

start line 다음에 나오는 부분은 헤더다. 헤더 정보는 HTTP 요청 그 자체에 대한 정보를 담고있다. 예를 들어, HTTP요청 메시지의 전체크기 같은 정보를 담고 있다.

 

헤더는 파이썬의 dictionary처럼 key와 value로 되어 있다. 그리고 key와 value는 : 로 연결된다. 즉, key:value로 표현이 된다. 

 

3)Body

HTTP 요청 메시지에서 body부분은 HTTP요청이 전송하는 데이터를 담고있는 부분이다. 전송하는 데이터가 없다면 body부분은 비어있게 된다. 

 

 

HTTP 응답구조

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>

1)Status Line

HTTP응답 메시지의 상태를 간략하게 요약하여 알려주는 부분이다. 다음과 같은 형태로 구성되며, HTTP 요청의 start line과 마찬가지로 status line도 다음과 같은 세부분으로 구성되어 있다. 

HTTP/1.1 404 Not Found

HTTP version

은 HTTP 요청 메시지의 start line과 마찬가지로 사용되고 있는 HTTP 버전을 말한다. 

 

status code

HTTP 응답 상태를 미리 지정되어 있는 숫자로 된 코드로 나타내준다. 예를 들어, HTTP 요청이 정상적으로 처리가 되었으면 응답의 status code는 200이라는 숫자로 표현된다. 

 

status text

HTTP 응답 상태를 간략하게 글로 설명해 주는 부분이다. 아무래도 숫자로 표현되어 있는 status code만으로는 항상 응답의 상태를 사람이 파악하기 어려울 수 있으므로 그 부분을 보완해 주는 역할을 한다고 생각하면 된다. 예를들어 HTTP요청이 정상적으로 처리한 HTTP 응답의 status code는 200이 되고 status text는 "OK"가 된다.

 

2)헤더

HTTP 응답의 헤더 부분은 HTTP 요청의 헤더 부분과 동일하다. 다만 HTTP 응답에서 사용되는 헤더 값들이 있다. 예를 들어 HTTP 응답에는 User-Agent 헤더 대신에 Server 헤더가 사용된다.

Connection: close
Content-Length: 1573
Content-Type: text/html; charset=UTF-8
Date: Mon, 20 Aug 2018 07:59:05 GMT

 

3)Body

HTTP응답 메시지의 body도 HTTP 요청 메시지의 body와 동일하다. 그리고 요청 메시지와 마찬가지로 전송하는 데이터가 없다면 body 부분은 비어 있게 된다. 

<!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>