Running Swagger-UI on different port

12,347 views
Skip to first unread message

JimB

unread,
Jan 16, 2013, 5:48:01 PM1/16/13
to swagger-sw...@googlegroups.com
I'm running 2 Apache virtual servers on different ports.  Port 8008 is hosting the Swagger-UI, and port 8100 is hosting the actual API.

I think everything is set up correctly.  The Swagger-UI looks correct in the browser, and when I test a simple get method, it shows the request URL properly.  I can copy/paste the generated request URL into another browser window and it loads the response properly.  However I'm not getting a response in Swagger-UI.  Specifically, the "Response Body" and "Response Headers" sections in Swagger-UI shows as empty.  The response code shows a "0".

When I combine the Swagger-UI and the actual API on the same virtual server (8100), Swagger-UI runs properly.

I've checked the Apache error log - nothing.  I've checked the Apache access log and I see the request coming through - it's an identical request/response to the manual request which works.

The only settings that I'm changing are:
In "index.html" at the SwaggerUi instantiation, change the port on discoveryUrl to 8008
In resources.json (as referenced by the discoveryUrl, change the port on the basePath to 8008 (as this basepath is referencing the other description files used in SwaggerUI).
In the description files, the basePath continues to reference 8100 (as this is the basepath of the API).

Any ideas why the SwaggerUI is not showing the response, even though my Apache logs show the request and response?


Also, BTW, I haven't used Swagger-UI since before the 2.0 release.  I noticed a number of things are improved.  I especially like being able to see the JSON models in the UI.

tony tam

unread,
Jan 16, 2013, 5:59:50 PM1/16/13
to swagger-sw...@googlegroups.com
Hi Jim,
My guess is that your API server is not supporting CORS.  When run across host:port combos, the API needs to return the following:

Access-Control-Allow-Methods: POST, GET, OPTIONS , PUT
Access-Control-Allow-Origin: *
If you are using chrome or firefox, and show the developer console, look for errors from the server, like "Origin null is not allowed by Access-Control-Allow-Origin."

Can you curl your server like this:
curl -i http://YOUR_SERVER:8100/PATH_TO_RESOURCE_LISTING
and post back the response?

Also, glad you're liking the progress in the UI!

Tony

JimB

unread,
Jan 16, 2013, 7:00:28 PM1/16/13
to swagger-sw...@googlegroups.com
Tony, Thanks for your quick response.

Looks like that was at least part of the problem.  I couldn't find the "origin null..." error in the browser, but I added the fields to the response header and now the Swagger-UI is showing the response.

One thing that is still odd is that the "Response Headers" section of the UI is still blank.  (The "Response Body" is filled though.)

My headers now are being returned as...
HTTP/1.1 200 OK
Date: Wed, 16 Jan 2013 23:49:59 GMT
Server: Apache/2.2.15 (Unix) PHP/5.3.2 mod_ssl/2.2.15 OpenSSL/0.9.8p
Expires: Wed, 16 Jan 2013 23:50:00 GMT
Pragma: no-cache
Access-control-allow-origin: *
Access-control-allow-methods: GET, POST, PUT, DELETE, OPTIONS, HEAD
Transfer-Encoding: chunked
Content-Type: application/json

jonathan...@safe.com

unread,
Oct 16, 2013, 3:00:42 PM10/16/13
to swagger-sw...@googlegroups.com
It looks like the generated XHR request doesn't honor a custom port and defaults to "80" but it's not clear if its the responsibility of "shred.bundle.js" or "swagger.js" to control this.

tony tam

unread,
Oct 16, 2013, 3:02:38 PM10/16/13
to swagger-sw...@googlegroups.com
Hi Jonathan, It shouldn't matter what port you use, can you share some more details?

Andrew Ralston

unread,
Oct 23, 2013, 4:01:53 PM10/23/13
to swagger-sw...@googlegroups.com
I'm running locally on 8080 and the UI is defaulting to port 80. Seems to be happening in shred.bundle.js:Object.defineProperties

        // - **port**. Unless you set the `port` explicitly or include it in the URL, it
        //   will default based on the scheme.
        
        port: {
            get: function() {
                if (!this._port) {
                    switch (this.scheme) {
                        case "https":
                            return this._port = 443;
                        case "http":
                        default:
                            return this._port = 80;
                    }
                }
                return this._port;
            },
            set: function(value) {
                this._port = value;
                return this;
            },
            enumerable: true
        },

Apparently it's configurable, but I'm not sure how.

Andrew

Simon Roberts

unread,
Oct 23, 2013, 6:21:28 PM10/23/13
to swagger-sw...@googlegroups.com
Bump... This is exactly the problem that I'm having. I have a fresh download of Swagger UI from two days ago. This aspect all used to work with an older version, but that had bugs that broke it for other reasons.

I know that the problem (with mine) is that the UI is using port 80 for service invocation because I restarted my server on port 80 and that made it work. By the way, in my case, the UI support and the service are both running on the same server on the same base URI (protocol, host, port, and servlet context root).

Any help would be much appreciated...

tony tam

unread,
Oct 23, 2013, 10:42:55 PM10/23/13
to swagger-sw...@googlegroups.com
Hi, I want to help, but I simply cannot reproduce this.  I'm sure it's something simple.

Take for example this sample:


(you can run the sample by executing node static-server.js)

This launches a web server on port 8000, and swagger-ui is served up from http://localhost:8000/swagger-ui  You can put in any URL to the UI (I've tested with port 8000, 8002, 8080) and there's no issue.

So I must be misunderstanding the issue.  @Andrew, what are you putting for the URL in the swagger-ui?

Andrew Ralston

unread,
Oct 24, 2013, 7:59:21 AM10/24/13
to swagger-sw...@googlegroups.com
I cloned your repo and tried your example - it worked for me after moving to port 8001 (had something else running on 8000). In the process of moving to 8001 I realized that the JSON you return for each high level API contains the full URL to use for the XHRs. My API JSON did not include the URL:port (e.g. "basePath": "/", vs "basePath": "http://localhost:8080",).  This is on the subsequent XHR requests for each high level API.

The other issue I noticed is that I was using an applicationContext (base of the site was localhost:8080/swaggerDemo/ instead of localhost:8080/). I had a problem right off the bat in index.gsp where the application context was not included in the url used for swagger: url: "${grailsApplication.config.grails.serverURL}/api-docs". I originally had changed this to url: "${grailsApplication.config.grails.serverURL}/swaggerDemo/api-docs" but changed it back after converting the application to the root context.

My application is using Grails 2.3.0 with the JAXRS and swagger4jaxrs plugins:

compile ":swagger4jaxrs:0.1"
compile (':jaxrs:0.10-SNAPSHOT') {
excludes 'spring-core', 'spring-beans', 'spring-context', 'spring-web', 'spring-aop'
}

The change I made to get this going was in applicationContext.xml:

<bean id="swaggerConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
        <property name="resourcePackage" value="swaggerDemo"/>
        <property name="version" value="0.01"/>
        <property name="basePath" value="/"/> <<<<<<------ change to <property name="basePath" value="http://localhost:8080"/>
        <property name="title" value="REST API"/>
        <property name="description" value="a description here"/>
        <property name="contact" value="em...@addr.com"/>
        <property name="license" value="Apache 2.0 License"/>
        <property name="licenseUrl" value="http://www.apache.org/licenses/LICENSE-2.0.html"/>
        <property name="scan" value="true"/>
        </bean>

This got things going for me, however I'm going to have to figure out how not to hard code the URL in the basePath property of the swaggerConfig PoJo in applicationContext.xml.

Andrew Ralston

unread,
Oct 24, 2013, 4:51:00 PM10/24/13
to swagger-sw...@googlegroups.com
This pull request solved my problem nicely:

priyanks...@gmail.com

unread,
Jul 10, 2018, 12:56:18 PM7/10/18
to Swagger

Hey,
How are you running Swagger UI and your own API on the same virtual port?

Can you guide me, please?

Mark Caasi

unread,
Jul 12, 2018, 9:30:56 PM7/12/18
to Swagger
I running Web Api in Docker and i get an error like this can help me please! thanks
 

Curl

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d 'login' 'http://192.168.78.205/Login'

Request URL

Response Body

no content

Response Code

0

Response Headers

{
"error": "no response from server"
}
Reply all
Reply to author
Forward
0 new messages