[Python] GET, POST 기본개념

doyeonlee·2022년 2월 9일
0

개발일지 2022

목록 보기
16/16
post-thumbnail

Get 요청, POST 요청

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

:예를 들어, 클라이언트에서 서버에 title_give란 키 값으로 데이터를 들고왔다고 가정

*GET 요청 API코드

@app.route('/test', methods=['GET'])
def test_get():
   title_receive = request.args.get('title_give')
   return jsonify({'result':'success', 'msg': '이 요청은 GET!'})

/test에서 GET을 실행
title_give를 title_receive로 받고, 메세지를 띄워라 '이 요청은 GET!'

*GET 요청 Ajax코드

$.ajax({
    type: "GET",
    url: "/test?title_give=봄날은간다",
    data: {},
    success: function(response){
       console.log(response)
    }
  })

/test에서 title_give=봄날은간다 를 읽어온다.
성공한다면, response라는 function을 실행

여기서, console.log()는 콘솔에 찍어 확인하기 위해서 만들어 놓은 코드이다.


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

:예를 들어, 클라이언트에서 서버에 title_give란 키 값으로 데이터를 들고왔다고 가정

*POST 요청 API코드

@app.route('/test', methods=['POST'])
def test_post():
   title_receive = request.form['title_give']
   return jsonify({'result':'success', 'msg': '이 요청은 POST!'})

title_receive는 title_give를 request.form으로 받고
받으면 '이 요청은 POST!'이라는 메세지를 띄워라.

*POST 요청 확인 Ajax코드

$.ajax({
    type: "POST",
    url: "/test",
    data: { title_give:'봄날은간다' },
    success: function(response){
       console.log(response)
    }
  })

title_give:'봄날은간다'의 데이터를 등록한다. 성공하면, response라는 function을 실행시켜라.

마찬가지로 console.log는 콘솔로 잘 돌아가는지 확인하기 위해서 넣어놓은 것이다.


profile
느려도 천천히 꼼꼼하게 !

0개의 댓글