【问题标题】:Swagger UI add curl custom parametersSwagger UI 添加 curl 自定义参数
【发布时间】:2018-10-04 11:35:23
【问题描述】:

Swagger UI 拒绝使用 自签名证书https 发出请求。

问题来了:

curl -X POST "https://localhost:8088/Authenticate" -H "accept: pplication/json" -H "Content-Type: application/json" -d "{ \"username\":"user\", \"password\": \"user\"}"

上面的命令是swagger自动生成的,运行后返回:

TypeError: Failed to fetch

手动(不使用 Swagger UI)运行返回:

curl: (60) SSL certificate problem: self signed certificate
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.


我想让它像下一个(添加 --insecure 参数):

curl -X POST "https://localhost:8088/Authenticate" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"username\": \"user\", \"password\": \"user\"}" --insecure

这将允许我执行所需的请求。有没有办法向自动生成的 Swagger curl 添加自定义参数?谢谢。

【问题讨论】:

    标签: swagger swagger-ui openapi


    【解决方案1】:

    首先,cURL 命令仅用于显示和复制粘贴。 Swagger UI 实际上并不使用 cURL 来处理请求——它是一个网页,所以它使用 JavaScript(fetch APIXMLHttpRequest 或类似的)发出请求。

    正如here 解释的那样,Swagger UI 不支持自签名证书(强调我的):

    您的证书似乎有问题。 出于我们无法控制的安全原因,Swagger-UI(或任何浏览器内的应用程序)无法绕过浏览器内置的证书验证过程。

    您似乎拥有自签名证书。您需要了解如何让您的计算机信任您的证书,这是 Google Chrome 的指南,看起来您正在使用它:

    Getting Chrome to accept self-signed localhost certificate

    因此,如果您想使用 Swagger UI 测试使用自签名证书的服务器,您需要将浏览器(或计算机)配置为信任该证书。查看这些问题以获取建议:

    Chrome: Getting Chrome to accept self-signed localhost certificate
    Firefox: Is there a way to make Firefox ignore invalid ssl-certificates?


    有没有办法为自动生成的 Swagger curl 添加自定义参数?

    生成 cURL 命令的代码在这里:
    https://github.com/swagger-api/swagger-ui/blob/master/src/core/curlify.js

    您可以分叉 Swagger UI 并根据需要调整代码。

    但如上所述 - 更改显示的 cURL 命令不会改变实际的“试用”行为,因为“试用”无法绕过证书验证。

    【讨论】:

      【解决方案2】:

      真正的问题是我的 OPTIONS 响应标头与我未来的 POST 请求标头不匹配,因为我必须使用 CORS

      【讨论】:

        【解决方案3】:

        我查看了这篇文章,因为我正在寻找在 API 中发出测试请求的最简单/智能的方法。 就这篇文章而言,在我看来,Swagger.io 没有使用 fetch API 或 XMLHttpRequest。

        我检查了以下代码: https://github.com/swagger-api/swagger-ui/blob/765056d1e4d28125f0dc4445ed9882531c3369ac/src/core/plugins/request-snippets/fn.js#L76

        我可以看到它使用 CMD 行 cURL.exe。所以,我想进一步问你......哪种方式最适合 Web 服务的 API 测试人员?既然它需要发出无法定义的请求,那么 fetch API 会得到它还是我们应该继续做“基本”?

        PS:不过,我还想补充一点,“nc”是一个不错的选择。 提前感谢任何有关此主题的 cmet。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-21
          • 1970-01-01
          • 2016-11-09
          • 2018-06-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多