Swagger UI 添加 curl 自定义参数

标签 swagger swagger-ui openapi

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 添加自定义参数?谢谢。

最佳答案

首先,cURL 命令仅用于显示和复制粘贴。 Swagger UI 实际上并不使用 cURL 来处理请求——它是一个网页,所以它使用 JavaScript(fetch APIXMLHttpRequest 或类似的)发出请求。
如解释 here , Swagger UI 不支持自签名证书(重点是我的):

It appears you have a problem with your certificate. There's no way for Swagger-UI (or any in-browser application) to bypass the certificate verification process built into the browser, for security reasons that are out of our control.

It looks like you have a self-signed certificate. You'll want to look into how to get your computer to trust your certificate, here's a guide for Google Chrome, which it looks like you're using:

Getting Chrome to accept self-signed localhost certificate


因此,如果您想使用 Swagger UI 测试使用自签名证书的服务器,则需要将浏览器(或计算机)配置为信任该证书。查看这些问题以获取建议:
Chrome : Getting Chrome to accept self-signed localhost certificate
火狐: Is there a way to make Firefox ignore invalid ssl-certificates?

Is there a way to add custom parameters to autogenerated Swagger curl?


生成 cURL 命令的代码在这里:
https://github.com/swagger-api/swagger-ui/blob/master/src/core/curlify.js
您可以 fork Swagger UI 并根据需要调整代码。
但如上所述 - 更改显示的 cURL 命令不会更改实际的“试用”行为,因为“试用”无法绕过证书验证。

关于Swagger UI 添加 curl 自定义参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49997748/

相关文章:

swagger - 如何使用 refs 在示例数组中放置多个项目 - Open API

c# - 如何在 ASP.NET Core 3.1 上运行的启用 OData 的 Web API 中添加 Swagger

swagger - 用于值验证的远程枚举列表 - 在 OpenAPI 规范中

maven - 适用于 OpenAPI 3.0 的 Swagger Codegen(带有 Maven 插件)

python - Flagger - 添加不记名授权

spring-boot - Swagger 为 REST 端点生成的主机名无效 REQUEST url

Swagger 正在将占位符文本添加到试用功能发送的请求中的 Url

spring-boot - 如何在 Springfox 中更改 Swagger UI index.html Logo 和标题内容?

amazon-web-services - 如何在请求之前在 Swagger 中计算 AWS 签名 V4

java - 如何从javadocs生成Swagger UI?