2 minute read

HTTP MESSAGE

HTTP message

Request

HTTP request 메시지는 크게 3분류로 구성된다.

  • Start line
    • HTTP Method 해당 request가 의도한 action을 정의하는 부분. HTTP Methods에는 GET, POST, PUT, DELETE, OPTIONS 등등이 있다. 주로 GET 과 POST과 쓰임.
    • Request target 해당 request가 전송되는 목표 uri. 예를 들어 /login.
    • HTTP Version 말 그대로 사용되는 HTTP 버젼. 버젼에는 1.0, 1.1, 2.0 등이 있다.
  • headers

    • 해당 request에 대한 추가 정보(addtional information)를 담고 있는 부분. 예를 들어, request 메세지 body의 총 길이 (Content-Length) 등.
    • Key:Value 값으로 되어있다 (: 이 사용됨). key:value HOST: google.com => Key = HOST, Value = google.com
    • Headers도 크게 3부분으로 나뉘지만(general headers, request headers, entity headers) 너무 자세한 부분임으로, 3부분 으로 구성되어 있다는것만 알고 있어도 괜찮다.
    Accept: */*
    Accept-Encoding: gzip, deflate
    Connection: keep-alive
    Content-Type: application/json
    Content-Length: 257
    Host: google.com
    User-Agent: HTTPie/0.9.3
    

    자주 사용되는 header 정보에는 다음이 있다:

    • Host 요청이 전송되는 target의 host url: 예를 들어, google.com
    • User-Agent 요청을 보내는 클라이언트의 대한 정보: 예를 들어, 웹브라우저에 대한 정보.
    • Accept 해당 요청이 받을 수 있는 응답(response) 타입.
    • Connection 해당 요청이 끝난후에 클라이언트와 서버가 계속해서 네트워크 컨넥션을 유지 할것인지 아니면 끊을것인지에 대해 지시하는 부분.
    • Content-Type 해당 요청이 보내는 메세지 body의 타입. 예를 들어, JSON을 보내면 application/json.
    • Content-Length: 메세지 body의 길이.
  • body

    • 해당 reqeust의 실제 메세지/내용. 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

Response도 request와 마찬가지로 크게 3부분으로 구성되어 있다.

  • Status line

    Response의 상태를 간략하게 나타내주는 부분. 3부분으로 구성되어 있다.

    • HTTP 버젼

    • Status code: 응답 상태를 나타내는 코드. 숫자로 되어 있는 코드. 예를 들어, 200

    • Status text: 응답 상태를 간략하게 설명해주는 부분.

      예를 들어, “Not Found”

      HTTP/1.1 404 Not Found

  • Headers

    • Response의 headers와 동일하다.
    • 다만 response에서만 사용되는 header 값들이 있다.
    • 예를 들어, User-Agent 대신에 Server 헤더가 사용된다.
  • Body

    • Response의 body와 일반적으로 동일하다.
    • Request와 마찬가지로 모든 response가 body가 있지는 않다. 데이터를 전송할 필요가 없을경우 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>
    

HTTP Methods

  • GET

    어떤 데이터를 서버로부터 받아올 때 사용하는 메서드. 데이터 생성/수정/삭제 없이 받아오기만 할 때 사용한다. 주로 데이터를 받아올 때 사용되기 때문에 request에 body를 안보내는 경우가 많다.

  • POST

    데이터를 생성/수정/삭제할 때 주로 사용되는 메서드.

  • OPTIONS

    주로 요청 URI에서 사용할 수 있는 메서드를 받아올 때 사용된다. 예를들어 /update uri에서 어떤 메서드를 요청 가능한가(GET? POST?)를 알고 싶으면 먼저 OPTIONS요청을 사용해 확인한다.

  • DELETE

    특정 데이터를 서버에서 삭제요청 할 때 쓰는 메서드.

  • PUT

    데이터를 생성할 때 사용되는 메서드. POST와 겹치기 때문에 최근에는 잘 사용되지 않는다고 한다.

HTTP Status Code

자세한 내용은 MDN 공신 문서를 참고하되 각 백 번대의 의미와 자주 보는 코드는 외워두자.

  • 1xx(정보) : 요청을 받았으며 프로세스를 계속 진행합니다.
  • 2xx(성공) : 요청을 성공적으로 받았으며 인식했고 수용하였습니다.
  • 3xx(리다이렉션) : 요청 완료를 위해 추가 작업 조치가 필요합니다.
  • 4xx(클라이언트 오류) : 요청의 문법이 잘못되었거나 요청을 처리할 수 없습니다.
  • 5xx(서버 오류) : 서버가 명백히 유효한 요청에 대한 충족을 실패했습니다.

Tags:

Updated: