kimjingyu 2023. 8. 10. 13:09
728x90

같은 예금 창구에서도 개인 고객이냐 기업 고객이냐에 따라 달리 처리하는 것처럼, 클라이언트가 요청할 때도 방식이 존재한다.

즉, HTTP 라는 통신 규약을 따르는데, 클라이언트는 요청할 때 HTTP request method를 통해서 어떤 요청 종류인지 응답하는 서버 쪽에 정보를 알려준다.

HTTP request method

GET, POST, PATCH, PUT, DELETE 등 여러 방식이 존재하고, 그 중 가장 많이 쓰이는 GET, POST 방식에 대해 리마인드해본다..

  • GET
    • 통상적으로 데이터 조회를 요청할 때 사용
    • 데이터를 전달하기 위해서 URL 뒤에 물음표를 붙여 key=value 형태로 전달한다.
  • POST
    • 통상적으로 데이터 생성, 변경, 삭제시에 사용한다.
    • 예) 회원가입, 회원탈퇴, 비밀번호 수정
    • 데이터를 전달하기 위해서 바로 HTML body에 key:value 형태로 전달한다.

GET, POST 요청에서 클라이언트의 데이터를 받는 방법

  • backend
@app.route('/test', methods=['GET'])
def test_get():
    title_receive = request.args.get('title_give')
    print('get 요청에 대한 응답:', title_receive)
    return jsonify({'result': 'success', 'msg': '이 요청은 GET 요청입니다.'})

@app.route('/test', methods=['POST'])
def test_post():
    title_receive = request.form['title_give']
    print('post 요청에 대한 응답:', title_receive)
    return jsonify({'result': 'success', 'msg': '이 요청은 POST 요청입니다.'})
  • frontend
<script type="text/javascript">
    function get_test(){
        $.ajax({
            type: "GET",
            url: "/test?title_give=봄날은간다",
            data: {},
            success: function(response){
                console.log(response)
            }
        })
    }

    function post_test() {
        $.ajax({
            type: "POST",
            url: "/test",
            data: { title_give: '어벤져스' },
            success: function(response) {
                console.log(response)
            }
        })
    }
</script>
728x90