Новое в Python 3.7 RESTFUl API. В следующей конечной точке POST я продолжаю получать следующее сообщение об ошибке в конечной точке POST, но конечная точка GET работает нормально.
Exception in _query: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
Интересно, как я могу это отладить? Нет четкого сообщения об ошибке в SQL или Python. Спасибо.
# all the imports
import os
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash
app = Flask(__name__) # create the application instance :)
app.config.from_object(__name__) # load config from this file , flaskr.py
def init_db():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
print('create database successfully')
db.commit()
# there are other codes in between being ignored
@app.route('/', methods=['GET'])
def show_entries():
try:
db = get_db()
# cur = db.execute('select * from mouse_tracking')
check_table="SELECT name FROM sqlite_master WHERE name = 'mouse_tracking' and type = 'table'; "
print(check_table)
cur = db.execute(check_table)
entries = cur.fetchall()
print(type(entries))
print(entries)
except sqlite3.Error as e:
print("Database error: %s" % e)
except Exception as e:
print("Exception in _query: %s" % e)
return render_template('show_entries.html', entries=entries)
@app.route('/add', methods=['POST'])
def add_entry():
try:
print('posting the content')
db = get_db()
db.execute('insert into mouse_tracking (user_id, date, x, y, click) values (?, ?, ?, ?, ?)',
[request.form['user_id'], request.form['date'], request.form['x'], request.form['y']
, request.form['click']])
db.commit()
flash('New entry was successfully posted')
except sqlite3.Error as e:
print("Database error: %s" % e)
except Exception as e:
print("Exception in _query: %s" % e)
return redirect(url_for('show_entries'))
Вот show_entries. html
{% extends "layout.html" %}
{% block body %}
{% if session.logged_in %}
<form action="{{ url_for('add_entry') }}" method=post class=add-entry>
<dl>
<dt>Title:
<dd><input type=text size=30 name=title>
<dt>Text:
<dd><textarea name=text rows=5 cols=40></textarea>
<dd><input type=submit value=Share>
</dl>
</form>
{% endif %}
<ul class=entries>
{% for entry in entries %}
<li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}
{% else %}
<li><em>Unbelievable. No entries here so far</em>
{% endfor %}
</ul>
{% endblock %}
вот вызов curl
curl --location --request POST 'http://127.0.0.1:5000/add' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic cm9vdDpkZWZhdWx0' \
--data-raw '{
"user_id": "1",
"date": "2020-01-01",
"x": "720",
"y": "50",
"click": "0"
}'
Вот схема. sql
create table if not exists mouse_tracking (
user_id integer not null,
date timestamp not null,
x integer not null,
y integer not null,
click integer not null
);