[TIL] HTTP_200408

이민석·2020년 4월 8일
0

TIL

목록 보기
8/14

HTTP

: HyperText Transfer Protocol

정의에 따라 HTTP는 HTML 문서를 서로 주고 받기 위한 통신 규약이다.

HTTP의 핵심 개념 2가지

  1. HTTP는 기본적으로 요청과 응답의 구조로 되어있다.
  2. HTTP는 Stateless, 즉 상태를 저장하지 않으므로 각각의 요청과 응답이 독립적이다.
    이 때문에 정보를 저장하거나 할때 쿠키나 세션을 활용하게 된다.

HTTP Request의 구조

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"
}
  • Start line
    : 가장 첫 줄. Method, target, http version으로 이루어져 있다. 여기서 method는 get, post, put과 같은 것들이고 target은 /login과 같은 것이다.

    GET /search HTTP/1.1

  • headers
    : request의 메타 데이타를 포함한다. 여러가지로 구성되지만 기본적인 구성은 target의 host url을 의미하는 Host, 요청을 보내는 클라이언트의 정보인 User-Agent 등이 포함된다. 요청이 이미 한번 처리 되었다는 내용을 포함해서 쿠키를 불러오는 첨부파일과도 같은 개념 또한 header에 포함된다.

    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가 없는 request도 많다.

 

HTTP Response의 구조

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>
  • Status line
    : Request의 start line과 마찬가지로 3부분으로 구성되어있지만 구성과 순서는 다르다. HTTP version, Status code, Status text로 이루어져있다.

    HTTP/1.1 404 Not Found

  • Headers
    : Request의 headers와 몇 부분만 제외하고 동일하다. 예를들어 user-Agent 대신 server 헤더가 사용된다.
  • Body
    : Request의 body와 일반적으로 동일하다. 마찬가지로 비어있는 body를 가질 수 있다.


Methods

Get

  • 서버로 부터 데이터를 받아올 때 사용된다.
  • 받아오기만 하므로 데이터의 생성,수정,삭제는 없고 request의 body에 내용 또한 없는 경우가 많다.

Post

  • 데이터를 생성, 수정, 삭제할 때 주로 사용되는 method.
  • get과는 달리 대부분의 경우 request의 body에 내용이 포함된다.

그 외

  • Options
  • Put
  • Delete

 

Status Code

200 OK

  • 문제없이 다 잘 실행 되었다

301 Moved Permanently

  • 해당 URI가 다른 주소로 바뀌었을 때

400 Bad Request

  • 해당 요청이 잘못 된 요청일 때 보내는 코드. 예를들어 번호여야 하는데 text일 때. 프론트엔드 탓

401 Unauthorized

  • 로그인을 하거나 회원가입을 해야하는 등 인증 자격 증명이 필요할 때

403 Forbidden

  • 유저가 해당 요청에 대한 권한이 없을 때. 예를들어 과금유저만 볼 수 있는 데이터

404 Not Found

  • 요청된 uri가 존재하지 않을 때

500 Internal Server Error

  • 서버에서 에러가 났을 때. 백엔드 탓
profile
Still learning

0개의 댓글