Swagger UI 添加 curl 自定义参数

Swagger UI add curl custom parameters

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 自动生成,在 运行 之后 returns :

TypeError: Failed to fetch

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

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 or 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:

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?


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

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

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

真正的问题是我的 OPTIONS 响应 headers 与我未来的 POST 请求 headers 不匹配,因为我不得不使用 CORS.

我检查了这个 post,因为我正在搜索 easiest/intelligent 如何在 API 中提出测试请求。 就此 post 而言,在我看来 Swagger.io 没有使用提取 API 或 XMLHttpRequest。

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

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

PS:不过,我还想补充一点,“nc”将是一个不错的选择。 感谢您提前对此主题提出任何意见。