July 2017
Intermediate to advanced
374 pages
8h
English
The POST method adds new tweets by a specified user.
Add the following code to app.py to add the route for the POST method for the tweets resource:
@app.route('/api/v2/tweets', methods=['POST'])
def add_tweets():
user_tweet = {}
if not request.json or not 'username' in request.json or not 'body' in request.json:
abort(400)
user_tweet['username'] = request.json['username']
user_tweet['body'] = request.json['body']
user_tweet['created_at']=strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
print (user_tweet)
return jsonify({'status': add_tweet(user_tweet)}), 200
Let's add the definition of add_tweet(user_tweet) to add tweets by a specified user, as follows:
def add_tweet(new_tweets): conn = sqlite3.connect('mydb.db') print ("Opened ...