본문 바로가기

study/깔끔한 파이썬 탄탄한 백엔드

정리 1

프론트, 백 통신할때 보통 http 프로토콜 사용한다. 

http 프로토콜은 html을 주고받는 약속

http 통신방식에는 두가지 특징이 있는데

1. request and response

2. stateless

 

flask에서는 response에 알아서 상태코드나 그런거 넣어서 보내줌

 

stateless는 각 통신마다가 독립적임 이전의 연결과 현재의 연결은 연관이 없다

그래서 로그인같은거는 쿠키나 세션 사용해야한다

 

 

 

http 요청

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, headers, body다

 

start line

start line에는 http 메소드, request target, http version으로 구성되어있다.

header

http 요청에 대한 정보

host주소, 클라이언트 정보 등등이 key, value 형태로 보내진다.

보면 accept에 body data type이나 contents lenght. connection(요청끝나고 계속 연결할건지 말건지) 등등 여러정보가 있음

body

요청이 전송하는 데이터를 담고 있다.

 

HTTP 응답

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, Headers, Body 다.

status line은 응답에 대한 상태를 간략하게 요약해서 알려주는 부분

나머지는 요청이랑 비슷함

 

 

자주사용하는 http 메소드

GET  POST OPTIONS(메소드 뭐 허용하는지 알려주는거)

 

 

자주사용되는 HTTP status code & Text

200 ok 문제없다는 뜻

301 Moved Permanetly 요청보낸 엔드포인트의 URL주소가 바뀌었다는 뜻

400 Bad Request 잘못된 요청

등등 있음

 

 

API 엔드포인트 아키텍처 패턴

크게 REST방식 GraphQL방식 이 있다.

RESTful 은 리소스를 URL로 표시하고 행동을 HTTP로 정의하는 방식이다. url만 봐도  바로바로 이해가능해서 좋다

GraphQL은 rest의 단점을 보완해서 나옴. REST방식은 기존의 만든 틀 내에서 사용해야해서 확장이나 뭐 이런게 힘듦

 

둘의 차이를 보자

만약 1인 유저의 정보와 그의 친구들의 정보를 받을라면

GET /users/1

GET /users/1/friends

이렇게 요청한다

 

근데 GraphQL은

POST /graphql

{

 user(id:1){

name

friends{

name

emaiol

}

}

}

이런식으로 가능