【问题标题】:Flask App: API returns the same response for different requestsFlask App:API对不同的请求返回相同的响应
【发布时间】:2020-01-20 19:19:33
【问题描述】:

我有一个具有多个 API 的 Flask 应用程序,我使用 POST 请求访问其中一个 API,并在正文中获取数据以及一些标头,例如 student_id。此 API 根据 headers 中传递的 student_id 返回学生结果。

我已经在 pycharm IDE(本地)中运行它,它可以 100% 运行,其中对于每个不同的 student_id,都会返回不同的结果。否则(student_id 未通过)返回错误请求。

但是当它被部署到 AWS (Elasticbean stalk) 中时,任何 student_id 的结果都是相同的,即使标头中没有 student_id,它仍然返回结果(在此 API 上发出的第一个请求的结果相同)。

在我看来,我想这是一个缓存问题,所以我尝试添加一个装饰器 @app.after_request 但它没有用

@application.after_request
def after_request(response):
    response.headers['Last-Modified'] = str(http_date(datetime.now() - timedelta(days=1)))
    response.headers["Cache-Control"] = "'no-store, no-cache, must-revalidate, post-check=0, 
    pre-check=0, max-age=0'"
    response.headers["Expires"] = '-1'
    response.headers["Pragma"] = "no-cache"
    return response

【问题讨论】:

    标签: python amazon-web-services api caching flask


    【解决方案1】:

    我已经解决了这个问题,我的问题中的上述代码在禁用缓存的情况下运行良好,但请确保对 HTTP 使用有效的日期格式(我已将代码修改为 100% 工作)。

    问题与缓存无关,这是某些标头未传递到服务器的问题,这是因为 AWS 服务器在 Nginx 和 Apache 上运行,并且

    标题中不允许使用下划线

    完整的细节参考这个问题underscores are not allowed in headers

    【讨论】:

      猜你喜欢
      • 2019-04-05
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2018-09-24
      相关资源
      最近更新 更多