swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Can't read from server. It may not have the appropriate access-control-origin settings. ?

Open interpegasus opened this issue 13 years ago • 29 comments

Hi,

The server returns the following message: "Can't read from server. It may not have the appropriate access-control-origin settings."

The API URL is: http://swagger-espn.mobovivo.com/api-docs.json

Could you please help to fix the issue?

Thanks in advance Screen Shot 2013-02-11 at 10 54 09 PM

SPEC FILE

{
"basePath": "http://espn-server.herokuapp.com/api",
"swaggerVersion": "0.1a",
"apiVersion": "1.0",
"apis": [
{
    "path": "/info.json",
    "format": "json",
    "description": "Get API info and current match",
    "operations": [
        {
            "httpMethod": "GET",
            "summary": "Get API info and current match",
            "nickname": "info",
            "tags": [
                "production"
            ],
            "deprecated": false
        }
    ]
},
{
    "path": "/info/{id}.json",
    "format": "json",
    "description": "Get API info and current event",
    "operations": [
        {
            "httpMethod": "GET",
            "summary": "Show Event Info.",
            "nickname": "showByIdOne",
            "tags": [
                "production"
            ],
            "deprecated": false,
            "parameters": [
                {
                    "name": "id",
                    "description": "id = 1",
                    "dataType": "integer",
                    "allowMultiple": false,
                    "required": true,
                    "paramType": "path"
                }
            ],
            "errorResponses": [
                {
                    "reason": "Not found",
                    "code": 404
                },
                {
                    "reason": "Invalid ID",
                    "code": 400
                }
            ]
        }
    ]
}
]
}

interpegasus avatar Feb 12 '13 04:02 interpegasus

Your server is not allowing CORS requests:

curl -i "http://swagger-espn.mobovivo.com/api-docs.json"
HTTP/1.1 200 OK
Date: Tue, 12 Feb 2013 05:14:27 GMT
Server: Apache/2.2.20 (Ubuntu)
Last-Modified: Tue, 12 Feb 2013 04:02:39 GMT
ETag: "738ec-154-4d57f18baad57"
Accept-Ranges: bytes
Content-Length: 340
Content-Type: application/json

In order to run the swagger-ui from a host other than mobovivo.com, you'll have to return the proper header values for Access-Control-Allow-Methods and Access-Control-Allow-Origin, like such:

curl -i "http://api.wordnik.com/v4/resources.json"
HTTP/1.1 200 OK
Access-Control-Allow-Headers: Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport, *
Access-Control-Allow-Methods: POST, GET, OPTIONS , PUT
Access-Control-Allow-Origin: *
Access-Control-Request-Headers: Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport,  *
Content-Type: application/json
Date: Tue, 12 Feb 2013 05:14:27 GMT
Content-Length: 319
Connection: keep-alive

fehguy avatar Feb 12 '13 05:02 fehguy

Thanks, I enabled CORS and it works! :+1:

interpegasus avatar Feb 12 '13 05:02 interpegasus

great news

fehguy avatar Feb 12 '13 05:02 fehguy

Could you please tell me how? I have a pretty standard webapp running on tomcat (6/7). I always get that message and I do not really understand what I have to do.

HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Wed, 08 May 2013 16:20:52 GMT

{"apiVersion":"0.1-ALPHA","swaggerVersion":"1.0","basePath":"http://localhost:9999/","apis":[{"path":"/api-docs/rest/api/order"}]}

gaffa avatar May 08 '13 16:05 gaffa

http://enable-cors.org/server.html or http://stackoverflow.com/questions/12048345/how-do-i-tell-tomcat-to-support-cors-for-static-content might help you.

JonnyJD avatar May 09 '13 23:05 JonnyJD

Just got mine working... Awesome!

marcellodesales avatar Dec 20 '13 10:12 marcellodesales

pls re-open this issue, I got mine working from localhost, but if I access it from another ip address it will return can't read from server.... swagger-ui-can-not-access localhost version swagger-ui-localhost And this is my response header from localhost :

Access-Control-Allow-Headers:Content-Type, api_key, Authorization, x-requested-with, Total-Count, Total-Pages, Error-Message
Access-Control-Allow-Methods:POST, GET, DELETE, PUT, OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1800

Am I missing something ?

Thanks in advance

toshim45 avatar Aug 19 '14 10:08 toshim45

@toshim45 - what's the output you're getting when running:

curl -i "http://localhost:8080/pena-im/api-docs"

webron avatar Aug 19 '14 10:08 webron

hi @webron

$ curl -i "http://localhost:8080/pena-im/api-docs"
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, DELETE, PUT, OPTIONS
Access-Control-Allow-Headers: Content-Type, api_key, Authorization, x-requested-with, Total-Count, Total-Pages, Error-Message
Access-Control-Max-Age: 1800
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 19 Aug 2014 19:47:47 GMT
{"apiVersion":"1.0","swaggerVersion":"1.2","apis":[{"path":"/default/compose-controller","description":"Compose Controller"},{"path":"/default/home-controller","description":"Home Controller"},{"path":"/default/inbox-controller","description":"Inbox Controller"}],"info":{"title":"default Title","description":"Api Description","termsOfServiceUrl":"Api terms of service","contact":"Contact Email","license":"Licence Type","licenseUrl":"License URL"}}

toshim45 avatar Aug 19 '14 19:08 toshim45

sorry my mistake, my configuration is wrong in index.html :) it should be ip address, not localhost. thanks @webron

toshim45 avatar Aug 20 '14 04:08 toshim45

You rock man!

gandarez avatar Sep 05 '14 14:09 gandarez

Hi Guyz, I just started using swagger, can you help me understand how do I use it to document my REST API's? Thanks in advance!!

jhambj avatar Dec 06 '14 12:12 jhambj

I got it downloaded from github , but now how to initiate to make it work for my own API's instead of petstore already existing one

jhambj avatar Dec 06 '14 12:12 jhambj

@jhambj - Please send a post to https://groups.google.com/forum/#!forum/swagger-swaggersocket with the details of your application, and I'll help you from there.

webron avatar Dec 06 '14 12:12 webron

just doing that but even I am not sure what all details are needed though leaving a post there.Thank you so much.

jhambj avatar Dec 06 '14 12:12 jhambj

Hi, I could solve this issue on Tomcat server, but it remains a problem on websphere application server profile as a server. Do you have any way to fix this?

Many thx :-)

SuperMarioX avatar May 22 '15 05:05 SuperMarioX

Not sure why websphere would be any different. Enabling CORS is normally done at the application level, though tomcat has its own filter you may have used. Just enable it on websphere and you should be done.

webron avatar May 22 '15 07:05 webron

http://stackoverflow.com/questions/24277953/how-to-define-cors-in-websphere-application-server-liberty-profile-v8-5 That helps.

SuperMarioX avatar May 22 '15 09:05 SuperMarioX

Why we need to accept CORS for fetching json file? you don't need it for AJAX GET

senid231 avatar Nov 25 '15 15:11 senid231

You don't need CORS for a GET. Swagger-js (the underlying library for the UI) does not differentiate in the request process and therefore sends a preflight request.

fehguy avatar Nov 28 '15 02:11 fehguy