烧瓶问题和错误的请求

Lar*_*rdt 4 flask flask-mysql angular5

我当时正在编程一个非常不错的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/5” HTTPStatus.BAD_REQUEST- 127.0.0.1--[2018年3月20日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?\ t \ 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()
Run Code Online (Sandbox Code Playgroud)

我的客户职能

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

Moh*_*rif 13

我也面临同样的问题

仅使用 http 而不是 https :-

http://ip:portnumber
Run Code Online (Sandbox Code Playgroud)

  • 我不明白这与普拉卡什一年前给出的答案有什么不同...... (3认同)

Pra*_*dey 10

我和你有同样的错误。

我的Flask服务器安装在内部respberry-pi,我正尝试使用进行访问https://ip:5000

问题是我使用https而不是http

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

  • 在类似的情况下,我被困在这个问题上 4 小时。如此简单,如此合乎逻辑。 (3认同)
  • stackoverflow的优点在于您的答案可以节省某人的时间。 (2认同)
  • 我创建了一个帐户来感谢您 (2认同)

Gof*_*tty 6

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

更改SSL_DISABLE=FalseSSL_DISABLE=True

所以,这里的重点是:检查你的 URL,也许是这样的:https://127.0.0.1:5000,只需将其更改为http://127.0.0.1:5000.

希望它对将来也面临此问题的人有所帮助。