烧瓶问题和不良要求

时间:2018-03-20 16:29:59

标签: flask angular5 flask-mysql

我自己编写了一个非常好的api,用json从我的游戏服务器获取一些json数据到我的网站空间,

但每次我使用angular发送请求我都会得到:127.0.0.1 - - [20 / Mar / 2018 17:07:33]代码400,消息错误请求版本(“▒\x9c▒▒{▒ “\ X12 \x99▒▒▒\ xadH \ X00 \ X00 \x14▒+▒/▒,▒0▒\x13▒\ X14 \ X00 / \ X005 \ X00" ) 127.0.0.1 - - [20 / Mar / 2018 17:07:33]“▒\x9dtTc▒\x93▒4▒M▒▒▒▒▒\x9c▒▒{▒'\x99▒▒▒▒H▒+▒ /▒,▒0▒▒/ 5“HTTPStatus.BAD_REQUEST - 127.0.0.1 - - [20 / Mar / 2018 17:07:33]代码400,消息错误请求语法('\ x16 \ x03 \ x01 \x00▒\ x01 \ x00 \ x00 \ x9d \ x03 \x03▒k, &安培;▒▒ua\ x8c \ X82 \ X17 \x05▒QwQ$▒0▒▒\x9f▒B1\ X98 \x19W▒▒▒▒\ X00 \ X00 \x14▒+▒/▒,▒0▒\x13▒ \ X14 \ X00 / \ X005 \ X00' ) 127.0.0.1 - - [20 / Mar / 2018 17:07:33]“▒\x9d▒k,&▒▒ua\ x8c \x82▒QwQ$▒0▒▒\x9f▒B1\x98W▒▒▒▒ ▒+▒/▒,▒0▒▒/ 5“HTTPStatus.BAD_REQUEST - 127.0.0.1 - - [20 / Mar / 2018 17:07:33]代码400,消息错误请求语法('\ x16 \ x03 \ x01 \x00▒\ x01 \ x00 \x00▒\ x03 \ x03)▒▒\ X1E \xa0▒\吨\ r \ x14g%▒▒\x17▒▒\ X80 \ x8d}▒F▒▒\x08U▒ġ▒▒\x06▒\ X00 \ X00 \x1c▒+▒/▒,▒0▒ “) g%▒▒▒▒\ x80 \ x8d}▒F▒U▒ġ▒▒▒▒+▒/▒,▒0▒“HTTPStatus.BAD_REQUEST -

我的api

from flask import Flask, jsonify
from flaskext.mysql import MySQL
from flask_cors import CORS, cross_origin

app = Flask(__name__)
CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
cors = CORS(app, resources={r"/punishments": {"origins": "http://localhost:5000" "*"}})
mysql = MySQL()

# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'test'
app.config['MYSQL_DATABASE_PASSWORD'] = 'Biologie1'
app.config['MYSQL_DATABASE_DB'] = 'test'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'

mysql.init_app(app)

@app.route('/punishments', methods=['GET'])
@cross_origin(origin='localhost:5000',headers=['Content- Type','Authorization'])
def get():
    cur = mysql.connect().cursor()
    cur.execute('''select * from test.punishments''')
    r = [dict((cur.description[i][0], value)
              for i, value in enumerate(row)) for row in cur.fetchall()]
    return jsonify({'punishments' : r})

if __name__ == '__main__':
    app.run()

我的客户端功能

export class ApiUserService {

  private _postsURL = "https://localhost:5000/punishments";

  constructor(private http: HttpClient) {
  }

  getPosts(): Observable<Punishments[]> {

    let headers = new HttpHeaders();
    headers = headers.set('Content-Type', 'application/json; charset=utf-8');

    return this.http
      .get(this._postsURL,{
        headers: {'Content-Type':'application/json; charset=utf-8'}
      })
      .map((response: Response) => {
        return <Punishments[]>response.json();
      })
      .catch(this.handleError);
  }

  private handleError(error: Response) {
    return Observable.throw(error.statusText);
  }
}

4 个答案:

答案 0 :(得分:5)

我和你有同样的错误。

我的烧瓶服务器安装在respberry-pi内,而我试图使用https://ip:5000访问它。

问题是我使用的是https而不是http

当我将其更改为http://ip:5000时,它起作用了。

答案 1 :(得分:0)

我也遇到了同样的问题

仅使用http,而不使用https:-

http://ip:portnumber

答案 2 :(得分:0)

最近我也遇到了这个问题,问题出在我应用程序的SSL配置上。 在我的 .env 中,将SSL_DISABLE设置为False,然后将其更改为True

SSL_DISABLE=False更改为SSL_DISABLE=True

因此,这里的重点是:检查您的URL,也许是诸如https://127.0.0.1:5000之类的东西,只需将其更改为http://127.0.0.1:5000

希望它对将来也会遇到此问题的人有所帮助。

答案 3 :(得分:0)

就我而言,我试图调试在 Flask 上运行的 SocketIo 服务器。我试图使用导致错误请求的 wss:// 访问服务器。将其更改为 ws:// 解决了问题。

相关问题