로컬개발환경/flask
API 만들기 (get, post)
class="멋쟁이"
2022. 6. 3. 18:04
GET = 통상적으로 테이터 조회를 요청할때
URL 뒤에 물을표를 붙여 KEY=VALUE로 전달
예) google.con?q=북극곰
POST = 통상적으로 데이터 생성, 변경, 삭제요청을 할때
바로 보이지 않는 HTML body에 key:value형태로 전달
api만들기
GET
get요청 api 코드
@app.route('/test', methods=['GET']) def test_get(): title_receive = request.args.get('title_give') print(title_receive) return jsonify({'result':'success', 'msg': '이 요청은 GET!'})
|
cs |
get요청 확인 ajax코드(콘솔창에 입력)
$.ajax({
type: "GET",
url: "/test?title_give=봄날은간다",
data: {},
success: function(response){
console.log(response)
}
})
|
cs |
response = 서버에서 내려주는 값

POST
post 요청 api 코드
@app.route('/test', methods=['POST'])
def test_post():
title_receive = request.form['title_give']
print(title_receive)
return jsonify({'result':'success', 'msg': '이 요청은 POST!'})
|
cs |
post 요청 확인 ajax코드
$.ajax({
type: "POST",
url: "/test",
data: { title_give:'봄날은간다' },
success: function(response){
console.log(response)
}
})
|
cs |
※internal server error = 서버쪽에 에러가 났다는 뜻