RESTful flask API - route 이해하기, url 경로 설정

반응형

route

  • 해당 경로의 특정 행동을 수행하는 기능

소스 코드

from flask import Flask

# API 서버를 구축하기 위한 기본 구조
app = Flask(__name__)

# API는 함수로 처리
@app.route('/hithere', methods=['GET'])
def hi_there() :
    return 'Hithere~'

@app.route('/add', methods=['GET'])
def add():
    data = 283 + 111
    return str(data) # 반환값은 문자열로 반환해야 함

if __name__ == '__main__' :
    app.run()

실행 후 화면

 

Not Found, 이유는?

  • API 서버에 루트 경로를 올바르게 입력하지 않았기 때문에 에러 출력

 

해결 방법

  • 루트에서 정의한 경로를 확인하고 올바른 주소 입력

 

  • route('/add')

< add >

  • route('/hithere')

< hithere >

 

 

반응형