- 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)
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기