Jan-29-2018, 03:43 PM
I'm still learning python so I could be missing something obvious. I've followed a bunch of different tutorials but nothing seems to be showing me how to fix my specific issue.
I'm trying to POST data from main.py (API caller) to hello.py (API server). Expected result would be hitting 127.0.0.1:8080/message and seeing the newly added POST data. In this case the message "You did it!" would be the line displayed.
What I'm seeing from hello.py is this error: 127.0.0.1 - - [29/Jan/2018 09:29:38] "POST /message/new HTTP/1.1" 400 -
Everything can be viewed on Git.
main.py
I'm trying to POST data from main.py (API caller) to hello.py (API server). Expected result would be hitting 127.0.0.1:8080/message and seeing the newly added POST data. In this case the message "You did it!" would be the line displayed.
What I'm seeing from hello.py is this error: 127.0.0.1 - - [29/Jan/2018 09:29:38] "POST /message/new HTTP/1.1" 400 -
Everything can be viewed on Git.
main.py
import requests, json desc = "You did it!" headers = {'Content-Type': 'application/json'} url = "http://127.0.0.1:8080/message/new" body = {'message': desc} r = requests.post( headers=headers, url=url, data=body )hello.py
from flask import Flask, jsonify, request, g import sqlite3 app = Flask(__name__) conn = sqlite3.connect('TheBats.db') c = conn.cursor() @app.route('/') def hey_there(): return 'Who are you? I\'m Batman' @app.route('/message') def get_message(): conn = sqlite3.connect('TheBats.db') cur = conn.cursor() cur.execute('SELECT * FROM batquote') entries = cur.fetchall() return jsonify(entries) @app.route('/message/new', methods=['POST']) def post_message(): batquote = request.json['message'] conn = sqlite3.connect('TheBats.db') cur = conn.cursor() cur.execute('INSERT INTO batquote (message) VALUES(?)', (batquote,)) conn.commit() return cur.lastrowid #@app.route('/message/<int>') #def get_messageID(): # return jsonify(placeholder2) if __name__ == '__main__': app.run(debug=True, port=8080)