首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >flask和错误请求的问题

flask和错误请求的问题
EN

Stack Overflow用户
提问于 2018-03-21 00:29:59
回答 4查看 27.9K关注 0票数 39

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

但每次我使用angular发送请求时,我都会收到这样的消息: 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代码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▒“message -

我的api

代码语言:javascript
复制
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()

我的客户端函数

代码语言:javascript
复制
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);
  }
}

EN

回答 4

Stack Overflow用户

发布于 2018-08-05 00:02:03

我犯了和你一样的错误。

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

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

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

票数 93
EN

Stack Overflow用户

发布于 2019-08-09 01:48:14

我也面临着同样的问题。

只使用http而不使用https :-

代码语言:javascript
复制
http://ip:portnumber
票数 15
EN

Stack Overflow用户

发布于 2019-11-21 10:14:44

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

SSL_DISABLE=False更改为SSL_DISABLE=True

所以,这里的重点是:检查你的网址,可能是这样的:https://127.0.0.1:5000,只需将其更改为http://127.0.0.1:5000

希望对将来同样面临这个问题的人有所帮助。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49389535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档