HTTP 프로토콜은 World Wide Web(WWW) 에서 데이터 통신의 기초가되는 통신 방식이다. HTTP 프로토콜로 안에서 URL으로 부터 데이터를 얻는 여러가지 방식이있다. 다음 테이블은 다양한 HTTP methods 들을 요약하였다.
| Methods | Description |
|---|---|
GET | Sends data in unencrypted form to the server. Most common method. |
HEAD | Same as GET , but without response body |
POST | Used to send HTML form data to server. Data received by POST method is not cached by server. |
PUT | Replaces all current representations of the target resource with the uploaded content. |
DELETE | Removes all current representations of the target resource given by a URL |
Flask 라우트는 디폴트로 GETmethod로 라우팅한다. 다른 HTTP methods을 사용하기 위해서는 route() 데코레이터에 argument를 제공해주어야한다.
다음 코드는 GET 그리고 POST methods을 구현한 예시 코드이다.
from flask import Flask, request
import json
app = Flask(__name__)
@app.route('/get_example')
def get_example():
return 'welcome get method'
@app.route('/post_example',methods = ['POST'])
def post_example():
data = json.loads(request.data)
data['msg'] = 'welcome post method'
return data
if __name__ == '__main__':
app.run(debug = True)
위 코드를 작성 후 서버를 실행하여 Talend API Tester 를 사용해보자.
| URL | Methods | Body | Test Demo |
|---|---|---|---|
| http://127.0.0.1:5000/get_example | GET | None | ![]() |
| http://127.0.0.1:5000/post_example | POST | { "user": "jacob" } | ![]() |