Problems with flask and bad request

给你一囗甜甜゛ 提交于 2020-05-25 17:18:52

问题


I was programming myself a pretty nice api to get some json data from my gameserver to my webspace using json,

but everytime i am sending a request using angular i am getting this: 127.0.0.1 - - [20/Mar/2018 17:07:33] code 400, message Bad request version ("▒\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] code 400, message Bad request syntax ('\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] code 400, message Bad request syntax ('\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 -

My 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()

My client function

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);
  }
}

回答1:


I had the same error as yours.

My flask server was installed inside respberry-pi and I was trying to access it using https://ip:5000.

The problem was I was using https instead of http.

When I changed it to http://ip:5000, it worked.




回答2:


I also faced same problem

use only http not https :-

http://ip:portnumber



回答3:


Recently I also faced this, the problem came from the SSL config on my app. In my .env I set the SSL_DISABLE to False, then I change it to True.

change SSL_DISABLE=False to SSL_DISABLE=True

So, the point here is are: check your URL, maybe be it something like: https://127.0.0.1:5000, just change it to http://127.0.0.1:5000.

Hope it helps to someone who also facing this issue in the future.



来源:https://stackoverflow.com/questions/49389535/problems-with-flask-and-bad-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!