简体   繁体   English

Flask 400 错误请求

[英]Flask 400 Bad Request

I am trying to post a question using the /questions api as shown in the code bellow.我正在尝试使用 /questions api 发布问题,如下面的代码所示。 Whenever I try calling request.get_json() to try and see the body, I get this error in postman below.每当我尝试调用 request.get_json() 来尝试查看正文时,我都会在下面的 postman 中收到此错误。 Does anyone have a solution?有没有人有办法解决吗?

import os
from flask import Flask, request, abort, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
import random

from models import setup_db, Question, Category, db

QUESTIONS_PER_PAGE = 10

def create_app(test_config=None):
  # create and configure the app
  app = Flask(__name__)
  setup_db(app)

 
  CORS(app, resources={r"/api/*": {"origins": "*"}})

  @app.after_request
  def after_request(response):
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization,true')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
    return response

  @app.route('/questions', methods=['POST'])
  def add_question():
    print(request.get_json())
    return(jsonify({"msg":1}))
  return app

在此处输入图像描述

What is your Flask version?您的 Flask 版本是多少? I would suggest you to update to latest Flask and try.我建议您更新到最新的 Flask 并尝试。

or if you don't want to upgrade try request.json() instead of request.get_json() ,或者如果您不想升级尝试request.json()而不是request.get_json()


print(request.json)
print(request.json["email"])
print(request.json["password"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM