Setup host and basePath dynamically

4,690 views
Skip to first unread message

Юра Каплун

unread,
Apr 27, 2015, 8:17:58 AM4/27/15
to swagger-sw...@googlegroups.com
Hi, guys.
I have one problem. I have one site with SwaggerUi and three APIs from different environments.  I did not specify host and base path because I need setup it dynamically when the user selects environment. But I don't know how can I do this. I tried to do something like this swaggerUi.api.host="myhost" but this didn't work. SwaggerUi uses the same host that the site uses. Can you help me, please?

tony tam

unread,
Apr 27, 2015, 9:37:58 AM4/27/15
to swagger-sw...@googlegroups.com
There are many different techniques.  Look at these samples:

Using spring configuration:



Using bootstrap servlet:

Юра Каплун

unread,
Apr 27, 2015, 10:19:07 AM4/27/15
to swagger-sw...@googlegroups.com
I'm using  ASP.NET MVC. Is there are any way to do this only via JS?

Josh Ponelat

unread,
Apr 27, 2015, 11:17:54 AM4/27/15
to swagger-sw...@googlegroups.com
Currently, the basePath + schema( the http part) get baked into the operations, when fetching and then compiling a spec.

I'm still playing around with it,
but it seems to be impossible at the moment to do this in swagger-ui,
I will of course stand corrected by others?

tony tam

unread,
Apr 27, 2015, 11:46:25 AM4/27/15
to swagger-sw...@googlegroups.com
I do believe you can leave `host` blank in your spec (2.0 only) and it'll be treated relative to where you load it from.

Ron Ratovsky

unread,
Apr 27, 2015, 11:54:51 AM4/27/15
to swagger-sw...@googlegroups.com
Yeah, but the OP is asking to set it via code because it is hosted at a different location than the API itself.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

Skip

unread,
Nov 20, 2015, 2:23:05 AM11/20/15
to Swagger
Any technique to get what OP was asking for? I posted yet another question on this as the links here were broken.
Thanks

tony tam

unread,
Nov 27, 2015, 8:48:25 PM11/27/15
to Swagger
There is a setHost and setBasePath available in the swagger-js, which is reference from swagger.ui.  From petstore:

window.swaggerUi.api.setBasePath('/new/base/path')

Same goes for host.  Is that what you're looking for?

Marcus Eklöf

unread,
Apr 7, 2017, 9:39:37 AM4/7/17
to Swagger
window.swaggerUi.api.setHost('host:port')

This worked perfectly for what I was looking for!

However, I recently tried out Swagger UI
3.0.4. Is there a way to set the Host in that version as well? Seems odd that the function would not be kept on that version?

Hareeqi M

unread,
May 5, 2017, 10:09:15 AM5/5/17
to Swagger


I just created this for swagger ui 3.x  i hope it works for you 

O haya

unread,
Dec 6, 2017, 12:32:36 PM12/6/17
to Swagger
Hi,

I am sorry to post on an older thread, but in the github linked page, what is "in the console"  referring to?  What console is it?

Thanks,
Jim

Ron Ratovsky

unread,
Dec 6, 2017, 9:56:02 PM12/6/17
to swagger-sw...@googlegroups.com

The browser’s web console.

--

You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to

O haya

unread,
Dec 7, 2017, 1:21:03 PM12/7/17
to Swagger
Cool!  I was able to get it working as snippets.

Thanks,
Jim

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages