Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot test Grape API (inside Rails 3) using online swagger UI. #19

Closed
diarmuidoconnor opened this issue Dec 19, 2012 · 2 comments
Closed

Comments

@diarmuidoconnor
Copy link

Hi,

I have a Rails 3 application with a REST API built using Grape. Some relevant code excerpts are as follows:

routes.rb

   require 'grape'
   Blog::Application.routes.draw do  
       mount Blog::API => "/"
   end

api.rb

  require 'grape'
  require 'grape-swagger'
  module Blog
      class API < Grape::API
       mount Blog::API1
      add_swagger_documentation
  end
  end

api1.rb

   require 'grape'
   module Blog
      class API1 < Grape::API
          format :json
          resource :weblogs do
               get do
                   Weblog.all
               end
          ……………………..

The API works fine when I test it with curl. I’ve deployed it to Cloud Foundry if you want to test it. The URL is http://blog-api.cloudfoundry.com and a sample curl request would be:
curl -i http://blog-api.cloudfoundry.com/weblogs

I’ve followed your instructions for adding grape-swagger and then try to explore it using the online swagger demo. Everything renders ok, however, when I test any part of my api using the demo, the response body is empty and the response code is 0; although the log file on the server-side shows the request being handled. Any thoughts as to what is wrong?

Many thanks, Diarmuid

@tim-vandecasteele
Copy link
Contributor

Have you tried to run swagger-ui on your localhost?

If you are using the online swagger-ui demo, you should enable CORS on your API.

You can do this by putting the below code in your API description

before do
    header['Access-Control-Allow-Origin'] = '*'
    header['Access-Control-Request-Method'] = '*'
end

Tim

@diarmuidoconnor
Copy link
Author

The code to support CORS solved my problem - thanks.

I will look at running swagger-ui locally next - may need some assistance there as well !

Thanks, Diarmuid

LeFnord pushed a commit that referenced this issue Mar 15, 2016
LeFnord added a commit that referenced this issue Feb 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants