- http request를 통해 데이터를 받아서 db에 저장
@app.route('/tweet', methods=['POST'])
def tweet():
user_tweet = request.json
tweet = user_tweet['tweet']
if len(tweet) > 300:
return '300자를 초과했습니다', 400
app.database.execute(text("""
INSERT INTO tweets (
user_id,
tweet
) VALUES (
:id,
:tweet
)
"""), user_tweet)
return '', 200
http -v POST localhost:5000/tweet id=4 tweet="My first tweet!"
mysql> select * from users;
+----+--------+--------------------+-----------------+-------------------------+---------------------+------------+
| id | name | email | hashed_password | profile | created_at | updated_at |
+----+--------+--------------------+-----------------+-------------------------+---------------------+------------+
| 3 | 현욱 | lego7407@naver.com | 1234 | hi! | 2021-02-18 23:21:06 | NULL |
| 4 | tester | test@gmail.com | 12345678 | hello my name is tester | 2021-02-21 12:34:37 | NULL |
+----+--------+--------------------+-----------------+-------------------------+---------------------+------------+
2 rows in set (0.00 sec)
mysql> select * from tweets;
+----+---------+-----------------+---------------------+
| id | user_id | tweet | created_at |
+----+---------+-----------------+---------------------+
| 4 | 4 | My first tweet! | 2021-02-21 12:34:58 |
+----+---------+-----------------+---------------------+
1 row in set (0.00 sec)
반응형
'SW ENGINEERING > Flask' 카테고리의 다른 글
API 개발하기(15) - REFACTORING (0) | 2021.02.21 |
---|---|
API 개발하기(14) - timeline 엔드포인트 (0) | 2021.02.21 |
API 개발하기(12) - MySQL 설정 & 회원가입엔드포인트 (0) | 2021.02.18 |
API 개발하기(11) - MySQL 다루기 (0) | 2021.02.17 |
API 개발하기(10) - MySQL 설치 (0) | 2021.02.17 |
최근댓글