【问题标题】:POST /api/sendimage 400 (BAD REQUEST) Flask restful API from browserPOST /api/sendimage 400 (BAD REQUEST) 来自浏览器的 Flask RESTful API
【发布时间】:2019-02-11 13:34:07
【问题描述】:

我正在使用

Python 3.6.3
烧瓶==1.0.2
Flask-Cors==3.0.7
Flask-RESTful==0.3.7

用于制作一个API,用于使用post方法收集tiff图像并保存在本地。

api = Api(app)
CORS(app)

 class CatchImage(Resource):
    @cross_origin(supports_credentials=True)
    def post(self):
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            if which_extenstion(filename) != "tiff":
                return json.dumps({
                    "id": None,
                    "error": "Unsupported file type"
                })

            else:
                unique_id, folder_path, save_path = get_unique_id(filename)
                try:
                    file.save(save_path)
                except FileNotFoundError:
                    LookupError("no uploads folder")
                convert_jpg_save(save_path)
                jpg_image_path = get_jpg_image_path(folder_path)
                img = [
                    url_for("send_image", filename=image)
                    for image in jpg_image_path
                ]

                return jsonify({
                    "filename ": file.filename,
                    "id": unique_id,
                    "urls": img,
                    "error": None
                 })

        return json.dumps({"error": "no file"})

api.add_resource(CatchImage, '/api/sendimage')

我已经尝试过使用 Postman 的 API,它工作得很好。但是当我尝试从浏览器访问 API 时,我得到了这个

POST http://127.0.0.1:5000/api/sendimage 400(错误请求)

相同的代码如下,由 Postman 生成。

var form = new FormData();
form.append("file", "/home/blue/Templates/test.tiff");

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://127.0.0.1:5000/api/sendimage",
  "method": "POST",
  "headers": {
    "cache-control": "no-cache",
    "Postman-Token": "4fdcc138-5567-4c4d-8f7d-8967d45b3c2a"
  },
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": form
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

我确实认为这与 CORS 或设置了一些我无法弄清楚的错误有关。
我想了解问题的可能原因,主要是可能的解决方案。提前感谢您的时间 - 如果我遗漏了任何内容,过分或过分强调特定点,请在 cmets 中告诉我。

【问题讨论】:

    标签: python ajax flask


    【解决方案1】:

    400 (BAD REQUEST) 表示发送的数据不是服务器预期的数据。我认为这与 CORS 无关。

    我建议在您的 post 函数中使用 pdb 来查找引发响应的位置。

    import pdb; pdb.set_trace()
    

    【讨论】:

      【解决方案2】:

      根据jQuery文档,你必须声明数据类型:

      $.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: success,
        dataType: dataType
      });
      

      可能会添加此帮助:

      dataType: "json",
      

      参考资料:

      Getting 400 bad request error in Jquery Ajax POST

      Jquery ajax post request not working

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-02
        • 1970-01-01
        相关资源
        最近更新 更多