Swagger UI add curl custom parameters

12,823

Solution 1

Firstly, the cURL command is for display and copy-pasting only. Swagger UI does not actually use cURL for requests – it's a web page so it makes requests using JavaScript (fetch API or XMLHttpRequest or similar).

As explained here, Swagger UI does not support self-signed certificates (emphasis mine):

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

So if you want to use Swagger UI to test a server that uses a self-signed certificate, you'll need to configure your browser (or your computer) to trust that certificate. Check out these questions for suggestions:

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?

The code to generate the cURL command lives here:
https://github.com/swagger-api/swagger-ui/blob/master/src/core/curlify.js

You can fork Swagger UI and tweak the code as your needs dictate.

But as explained above – changing the displayed cURL command won't change the actual "try it out" behavior, because "try it out" cannot bypass certificate validation.

Solution 2

The real problem was that my OPTIONS response headers did not match my future POST request headers, since I had to use CORS.

Share:
12,823
Vladyslav Nikolaiev
Author by

Vladyslav Nikolaiev

Updated on June 08, 2022

Comments

  • Vladyslav Nikolaiev
    Vladyslav Nikolaiev almost 2 years

    Swagger UI refuses to make a request to https with self signed certificate.

    The problem is next:

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

    Above command is generated by swagger automatically and after run it returns :

    TypeError: Failed to fetch
    

    Manually (not using Swagger UI) run 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.
    


    I want to make it like next (add --insecure parameter):

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

    This will allow me to perform a desired request. Is there a way to add custom parameters to autogenerated Swagger curl? Thanks.