[HTTP] 기초 - Response

배정규·2020년 7월 13일
0

http

목록 보기
2/2

1. HTTP Response 구조

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

  • Start line
  • Headers
  • Body

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

2. 자주 쓰이는 HTTP Methods

GET

  • 이름 그대로 어떠한 데이터를 서버로부터 받아(GET)올 때 주로 사용하는 Method.
  • 데이터 생성/수정/삭제 없이 받아오기만 할 때 사용된다.
  • 가장 간단하고 많이 사용되는 HTTP Method.
  • 언급한대로 주로 데이터를 받아올 때 사용되기 때문에 request 에 body 를 안 보내는 경우가 많다.

POST

  • 데이터를 생성/수정/삭제 할 때 주로 사용되는 Method.
  • 데이터를 생성 및 수정 할 때 많이 사용하기 때문에 대부분의 경우 request body가 포함되서 보내진다.

3. 알고 있으면 좋은 HTTP Methods

OPTIONS

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

OPTIONS / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 0
Host: example.org
User-Agent: HTTPie/0.9.3



HTTP/1.1 200 OK
Allow: OPTIONS, GET, HEAD, POST
Cache-Control: max-age=604800
Content-Length: 0
Date: Mon, 20 Aug 2018 08:37:45 GMT
Expires: Mon, 27 Aug 2018 08:37:45 GMT
Server: EOS (vny006/0450)

PUT

  • POST와 비슷하다. 데이터를 생성할 때 사용되는 Method.
  • POST와 겹치기 때문에 PUT 을 사용하는 곳도 있고 POST로 통일해서 사용하는 곳도 있는데, 최근 몇년 사이에 POST에 밀려서 잘 사용안되는 추세.

DELETE

  • 특정 데이터를 서버에서 삭제 요청을 보낼 때 쓰이는 Method.
  • PUT 과 마찬가지로 POST 에 밀려서 잘 사용안되는 추세.

4. 자주 쓰이는 HTTP Status Code

200 OK

  • 가장 자주 보게되는 status code.
  • 문제없이 다 잘 실행 되었을 때 보내는 코드.

301 Moved Permanently

  • 해당 URI 가 다른 주소로 바뀌었을 때 보내는 코드
HTTP/1.1 301 Moved Permanently
Location: http://www.example.org/index.asp

400 Bad Resqust

  • 해당 요청이 잘못된 요청일 때 보내는 코드.
  • 주로 요청에 포함된 잘못된 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

  • 서버에서 에러가 났을 때 사용되는 코드.
  • API 개발응ㄹ 하는 백앤드 개발자들이 싫어하는 코드.

Today I Leaned


제 글이 조금이라도 도움이 되었으면 좋겠습니다.
읽어주셔서 감사합니다.

Seize the day!


profile
Seize the day

0개의 댓글