“未提供规格";尝试通过HTTPS传递swagger.json时出错 [英] "No spec provided" error when trying to deliver swagger.json over HTTPS

查看:107
本文介绍了“未提供规格";尝试通过HTTPS传递swagger.json时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试使用Flask RestPlus通过HTTPS交付Swagger UI,则在根URL上只会看到未提供规格"错误消息,并且完整的Swagger UI永远不会加载.但是,如果我访问API端点,它们将按预期返回响应.

If I try to deliver the Swagger UI using Flask RestPlus over HTTPS, I see only the "No spec provided" error message at the root URL, and the full Swagger UI never loads. However, if I visit the API endpoints they return responses as expected.

在查看错误页面的源HTML时,我注意到正在从http://myhost/而不是https://myhost/

Looking at the source HTML for the error page, I noticed that swagger.json was being fetched from http://myhost/ rather than https://myhost/

我在 restplus Github问题中发现了完全相同的问题

I've discovered exactly the same issue on the restplus Github issues

我已经通过猴子补丁暂时解决了我的问题. Swagger UI加载完毕,然后查看HTML源代码,发现swagger.json确实是从https://myhost获取的.

I've fixed my issue temporarily with the monkey-patch mentioned on that page. The Swagger UI loads, and looking at the HTML source I see that swagger.json is indeed fetched from https://myhost.

为什么会发生这种情况,如何在没有猴子补丁的情况下修复它?

Why is this happening, and how can I fix it without the monkey-patching?

HTTPS由Cloudflare的灵活" HTTPS服务提供.

HTTPS is courtesy of Cloudflare's "flexible" HTTPS service.

我的应用程序位于经过如此配置的Nginx的后面,据我所知,并没有引起任何问题:

My app is behind Nginx which is configured thus, and hasn't been causing any issues as far as I'm aware:

...
http {
  ...
  server {
    location / {
      charset UTF-8;
      try_files $uri @proxy_to_app;
    }
    location @proxy_to_app {
      charset UTF-8;
      proxy_intercept_errors on;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://127.0.0.1:5000;
    }
  }
}

推荐答案

我在下面使用它来使其工作.您可以在下面的链接中查看稳定的示例.

I have used below to get it worked. You can view the stable example in below link.

http://flask-restplus.readthedocs.io/en/stable/example.html

from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)

这篇关于“未提供规格";尝试通过HTTPS传递swagger.json时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆