Session - HTTP

Sung Jun Jin·2020년 4월 8일
0

위코드 세션 정리

목록 보기
5/12

HTTP

  • HypterText Transfer Protocol : 하이퍼텍스트(HTML) 문서를 교환하기 위해 만들어진 protocol(통신규약)

  • 프론트앤드 서버와 클라이언트, 프론트앤드와 백앤드간의 통신에 사용된다.

  • TCP / IP 기반으로 되어있다.

HTTP 핵심

  • HTTP는 기본적으로 요청 / 응답 구조로 되어 있다.
  • 클라이언트가 HTTP request를 서버에 보내면 서버는 HTTP reponse를 보내는 구조
  • HTTP는 stateless이다. 말 그대로 state(상태)를 저장하지 않는다는 뜻, 즉 각각의 요청이 오면 그에 따른 독립적인 응답만 할뿐, 과거에 있었던 여러개의 요청 / 응답끼리 연결되어 있지 않다. 만일 요청과 응답의 진행과정이나 데이터가 필요할때는 예전에 통신을 주고받았던 데이터를 가지고 있는 쿠키나 세션 등등을 사용하게 된다. 예를 들어 사용자가 로그인된 여부(상태)를 알고 싶을 때는 기존에 로그인 요청을 처리한 응답에 대한 데이터를 요청해야 한다.

HTTP Request 구조

Start Line

  • HTTP request의 첫 라인. 3가지 부분으로 구성되어 있다.
    1. request가 의도한 action(GET,POST,DELETE,PUT,OPTIONS)을 정의하는 부분
    2. 주로 GET과 POST가 쓰인다.
  • Request target : 해당 request가 전송되는 목표 uri
  • HTTP Version
GET /search HTTP/1.1

Headers

  • 해당 request에 대한 메타 정보를 담고 있는 부분, Key와 Value로 구성되어 있다

  • 자주 사용되는 header 정보

    	1. Host : 요청이 전송되는 target의 host url. ex) google.com. Host와 Request target을 조합하면 고유 주소가 나온다. ex) google.com/search
    	2. User-Agent : 요청을 보내는 클라이언트(주체)에 대한 정보 ex) 웹브라우저에 대한 정보
    	3. Accept : 해당 요청이 받을 수 있는 response타입
    	4. Connection : 해당 request가 끝난후에 클라이언트와 서버가 계속해서 네트워크 커넥션을 유지할 것인지에 대한 여부
    	5. Content-Type : 해당 요청이 보내는 메세지 body 타입 ex)application/json
    	6. Content-Length : 메세지 body의 길이
GET /search HTTP/1.1

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

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"
}

HTTP Response 구조

  • Request와 마찬가지로 Status line, Headers, Body로 구성되어 있다

Status Line

  • 응답의 상태를 간략하게 나타내주는 부분, 3가지 부분으로 구성되어 있다.
    1. HTTP 버전
    1. Status code : 응답 상태를 나타내는 코드 ex) 200,401,400
    2. Status text : 응답 상태를 간략하게 설명해주는 부분 ex) Not Found
HTTP/1.1 404 Not Found

Headers

  • requests와 크게 다르지 않다. 응답에 대한 메타 정보가 저장된다. 하지만 response에서 사용되는 header 값들이 있다. ex) Server

Body

  • 데이터를 전송할 필요가 없을 경우 body가 없는 response도 있다. 나머지는 request의 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

주요 HTTP Methods

GET

  • 데이터를 가져올때 사용하는 메소드. 데이터의 수정,삭제없이 받아오기만 할때 사용된다.

POST

  • 데이터를 생성/수정/삭제 할때 주로 사용되는 Method. request에 body가 주로 포함되어 있다.

이외에 PUT과 DELETE가 있지만 주로 GET,POST가 많이 사용된다.

profile
주니어 개발쟈🤦‍♂️

0개의 댓글