HTTP

yoosg·2019년 12월 15일
0

HTTP

Hypertext Transfer Protocol : 데이터를 주고 받기위해 상호간에 정의한 통신규약.

HTTP특징

    1. Request & Response

      Web Browser가 Frontend에게 요청을 하고 파일을 받는다. 데이터 요청을 Backend API에게 한다. Frontend와 Backend가 직접 교환하는 것이 아닌 Browser가 주제가 되어 교환한다.

    1. Stateless

      상태를 저장하지 않는다. 요청이 오면 응답 할 뿐, 각각의 request와 response는 독립적이다.

HTTP Request 구조

요청 메세지는 크게 Start line, Header, Body로 나눠진다.

1. Start line

  • HTTP Method : Request가 의도한 action을 정의하는 부분 GET, POST, DELETE, OPTIONS등이 있고 주로 GET와 POST가 사용된다.
  • Request target : 해당 Request가 전송되는 목표 URI.
  • HTTP Version : http 버전.
GET / search HTTP/ 1.1

2. Headers : 요청에 대한 정보를 담는 곳.

  • Host : 요청이 전송되는 target의 host url(google.com)
  • User-Agent : 요청을 보내는 클라이언트에 대한 정보(웹브라우저에 대한 정보)
  • Accept : 해당 요청이 받을 수 있는 응답(Response) 타입.
  • Connection : 해당 요청이 끝난 후 클라이언트와 서버가 계속해서 네트워크 커넥션을 유지할 것인지 끊을 것인지 지시하는 부분.
  • Content-Type : 해당 요청이 보내는 메세지 body의 타입.
  • Content-Length : 메세지 body의 길이.
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json
Content-Length: 257
Host: google.com
User-Agent: HTTPie/0.9.3

3. Body : Request의 실제 내용. Python의 dictionary 형태. GET request들은 body가 없는 경우가 많다.

POST /payment-sync HTTP/1.1 (startline method/endpoint target/version)

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 구조

Response 구조는 Status line, Headers, Body로 이뤄진다.

1. Status line

Response의 상태를 간략하게 나타내는 부분.

  • HTTP 버전
  • Status code : 응답 상태를 나타내는 코드.
  • Status text : 응답 상태를 설명하는 부분.
HTTP/1.1 404 Not Found

2. Headers

  • Request의 헤더 역할과 동일하다. 단, User-Agent말고 Server 헤더가 사용된다.

3. Body

  • Reqeust 처럼 데이터를 전송할 필요가 없을 경우에 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 Status Code

  • 200 OK

    문제없이 다 잘 실행 되었을때 보내는 코드.

  • 301 Moved Permanently

    해당 URI가 다른 주소로 바뀌었을때 보내는 코드.

HTTP/1.1 301 Moved Permanently
Location: http://www.example.org/index.asp
  • 400 Bad Request

    해당 요청이 잘못된 요청일대 보내는 코드.
    주로 요청에 포함된 input 값들이 잘못된 값들이 보내졌을때 사용되는 코드.
    예를 들어, 전화번호를 보내야 되는데 text가 보내졌을때 등등.

  • 401 Unauthorized

    유저가 해당 요청을 진행 할려면 먼저 로그인을 하거나 회원 가입을 하거나 등등이 필요하다는것을 나타내려 할때 쓰이는 코드.

  • 403 Forbidden

    유저가 해당 요청에 대한 권한이 없다는 뜻.
    예를 들어, 오직 과금을 한 유저만 볼 수 있는 데이터를 요청 했을때 등.

  • 404 Not Found

    요청된 uri가 존재 하지 않는다는 뜻.

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
  • 500 Internal Server Error

    서버에서 에러가 났을때 사용되는 코드.

0개의 댓글