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 API 或 XMLHttpRequest 或类似的)发出请求。
如解释 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:
因此,如果您想使用 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/