Language&Framework/Django Rest Framework

DRF | 1주차 HTTP와 웹의 동작 방식

JUNG씨 2022. 10. 24. 20:45

HTTP(HyperText Transfer Protocol)

▪️ 원래는 HTML 전송용으로 나왔으나 현재는 모든 형태를 전송한다.

▪️ 이미지, 음성, 영상, 파일, JSON, XML etc..

 

 

HTTP의 특징

▪️ 클라이언트 서버구조

- request response 구조

- 클라이언트는 Request를 보내고 Response를 기다림.

- > 데이터와 비즈니스 로직을 전부 서버에 넣고 클라이언트에는 ui를 전부 넣는다 : 독립적인 진화 가능!

▪️ stateless(무상태 프로토콜)

- 서버가 클라이언트 상태를 보존하지 않는다. 

- 응답 서버를 쉽게 바꿀 수 있다.

▪️ 비연결성

- 연결 유지를 하지 않으면서 최소한의 자원을 사용할 수 있다.

- 기본적으로 연결을 유지하지 않는다.

- 초단위 이하의 빠른 응답 가능

- 효율적인 서버 자원 사용 가능

 

 

HTTP 메시지

 

 

 

HTTP Method

▪️ 안좋은 설계

- /read-member-list

- /read-member-by-id

- /create-member

- /update-member

- /delete-member

 

▪️ 좋은 설계 -> 리소스(회원이라는 개념 자체) 식별이 중요!

- 회원 목록 조회 /members

- 회원 조회 /members/{id}

- 회원 등록 /members/{id}

- 회원 수정 /members/{id}

- 회원 삭제 /members/{id}

 

 

➡️ 리소스(회원)와 행위(조회, 등록, 삭제, 변경)를 분리하는 것 : Restful API

 

▪️ Get : 조회

데이터는 쿼리스트링으로 전달

▪️ Post : 등록

- 메세지 바디를 통해 서버로 요청 데이터 전달

- 데이터 처리 / 프로세스 처리

- JSON으로 조회 데이터를 넘기고 싶은데 Get메소드를 쓰기 힘든 경우에 사용

 

▪️ Put : 대체 혹은 생성

- 파일 붙여넣기 = 없으면 만들고 있으면 덮어씀.

- 클라이언트가 URI를 지정해서 보냄.

 

▪️ Patch : 부분 변경

 

▪️  Head : Get과 동일하지만 상태줄과 헤더만 반환

 

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

 

HTTP request methods - HTTP | MDN

HTTP defines a set of request methods to indicate the desired action to be performed for a given resource. Although they can also be nouns, these request methods are sometimes referred to as HTTP verbs. Each of them implements a different semantic, but som

developer.mozilla.org

 

 

데이터 전송

➡️ 쿼리 파라미터 : Get (주로 검색, 정렬 필터) / 메세지 바디 : POST, PUT, PATCH (회원가입, 상품주문, 리소스 등록 변경 등)

 

▪️ HTML Form

- 회원가입, 주문, 데이터 변경

- Get, Post만 지원

- Content-Type: application/x-www-form-urlencoded 

➡️ 전송 데이터는 url encoding 처리

- Content-Type: multipart/form-data

➡️ 파일을 같이 보낼 때 사용. binary data 보낼 때. 다른 종류의 여러 파일과 폼의 내용을 함께 전송할 수 있다. 

 

▪️HTTP API

- 회원가입, 주문, 데이터 변경

- 서버 to 서버, 앱 클라이언트, 웹 클라이언트(ajax)

- Content-Type:application/json

- Form 대신 JS를 이용한 통신

- Post, Put, Patch도 메세지 바디로 데이터 전송 가능

 

 

HTTP 상태 코드

Status code의 첫 번째 숫자에 따라 response class가 달라진다.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

 

HTTP response status codes - HTTP | MDN

HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:

developer.mozilla.org

 

 

HTTP 헤더

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers

 

HTTP headers - HTTP | MDN

HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its case-insensitive name followed by a colon (:), then by its value. Whitespace before the value is ignored.

developer.mozilla.org