Swagger-ui only sending OPTIONS not POST http method despite working API
Asked Answered
T

5

8

I am using Swagger-UI to browse my own API, built with grape and automatically documented with grape-swagger.

I've googled and tried every suggestion I can find, but I cannot get POST to work. Here's my headers:

  header "Access-Control-Allow-Origin", "*"
  header "Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, PATCH, DELETE"
  header "Access-Control-Request-Method", "*"
  header "Access-Control-Max-Age", "1728000"
  header "Access-Control-Allow-Headers", "api_key, Content-Type"

I just threw in everything suggested. I've enabled all the HTTP methods in supportedSubmitMethods and I have tested the API using the POSTMAN Chrome extension and it works perfectly. Creates a user properly and returns the correct data.

However all I get with swagger post is the server reporting:

Started OPTIONS "/v1/users.json" for 127.0.0.1 at 2012-12-21 04:07:13 -0800

and swagger response looking like this:

Request URL

http://api.lvh.me:3000/v1/users.json

Response Body

Response Code

0

Response Headers

I have also tested the OPTIONS response with POSTMAN and it is below:

Allow →OPTIONS, GET, POST
Cache-Control →no-cache
Date →Fri, 21 Dec 2012 12:14:27 GMT
Server →Apache-Coyote/1.1
X-Request-Id →9215cba8da86824b97c6900fb6d97aec
X-Runtime →0.170000
X-UA-Compatible →IE=Edge
Tangy answered 21/12, 2012 at 12:15 Comment(4)
did you solve it? I'm stuck with the same problem.Broker
to be honest i cannot remember if i solved it or put it on the shelf - i'll take another look today and report back NehaTangy
it worked. I provided it a domain name and changed the basePath to the domain name.Broker
That's awesome news... so if you have an answer, post it, and I'll accept it.Tangy
M
3

I had the same problem and just solved it, hope this helps somebody.

Swagger-UI accepts multiple parameters through POST only through a 'form' paramType, not 'body' paramType, referenced in this issue https://github.com/wordnik/swagger-ui/issues/72.

I used the branch :git => 'git://github.com/Digication/grape-swagger.git' changing 'post' request paramType to 'form'. Generated xml output for swagger_doc (probably at path/swagger_doc/api or similar) should look something like this:

<api>
  <path>/api/v2/...</path>
    <operations type="array">
      ...
      <httpMethod>POST</httpMethod>
        <parameters type="array">
          <parameter>
            <paramType>form</paramType>
            ...More

Not

<paramType>body</paramType>
...More

I used the grape-swagger-rails gem to automatically install swagger-ui on localhost (files can also be downloaded from the swagger-ui site), and everything works!!

Miksen answered 10/5, 2013 at 21:22 Comment(0)
M
2

Had the same problem. Fixed by adding CORS

add into Gemfile:

gem 'rack-cors', :require => 'rack/cors'

add into application.rb

config.middleware.use Rack::Cors do
    allow do
      origins '*'
      # location of your API
      resource '/*', :headers => :any, :methods => [:get, :post, :options, :put]
    end
end

be sure that you've changed location of your API here.

Mannos answered 18/8, 2013 at 12:2 Comment(0)
G
0

Nice to hear you are using grape-swagger: I think it is awesome :)

I am not entirely sure you are having the same problem, but when testing locally from the browser it will try to check if the origin is the same as requested, so to make sure I do not get that error, I created a small middleware that will tell the browser we allow all origin.

I am using a rails process (created with the awesome rails-api gem), so I create a new file in lib/middleware/access_control_allow_all_origin.rb with the following content:

module Middleware
  class AccessControlAllowAllOrigin

    def initialize(app)
      @app = app
    end

    def call(env)
      status, headers, body = @app.call(env)
      allow_all_origin!(headers)
      [status, headers, body]
    end

    private

    def allow_all_origin!(headers)
      headers['Access-Control-Allow-Origin'] = '*'
      headers['Access-Control-Request-Method'] = '*'
    end

  end
end

and at the bottom of my application.rb I just add the middleware as follows:

require 'middleware/access_control_allow_all_origin'
config.middleware.insert_after Rack::ETag, Middleware::AccessControlAllowAllOrigin

Hope this helps.

Gimcrackery answered 8/1, 2013 at 22:46 Comment(1)
It is not the CORS issue that I'm getting - though that was an earlier problem that I'd had with this. I put this on the back burner to focus on other parts of the problem so I'll take a fresh look at this this week.Tangy
B
0

I do not know about the solution for ruby-on-rails as I am using Swagger with play framework 2.0.2. I provided a domain name to it and changed the basePath to domain name in application.conf file as swagger.api.basepath="domain-name" and it worked. You can change the basePath in api-docs to domain-name. I read about the api-docs on api-docs.

Broker answered 15/2, 2013 at 5:43 Comment(1)
oh - that's interesting - okay let me check this out then with my Rails implementation within the next 24 hours - if works I'll click the checkmark and upvote :-)Tangy
C
0

And does your web server hijack headers? If you are using NGinx for example, your "OPTIONS" request might not send the appropriate values as a response, in some cases.

What is your OPTIONS request response? Can you dump it out here? I'll tell you if it can be that.

Classless answered 12/4, 2013 at 5:36 Comment(1)
This is a good question. I haven't ignored it. Just need to get a chance to check as I've not looked at this in a whole. The testing was just local host and I cannot recall which server I was using for that - ill check and update this.Tangy

© 2022 - 2024 — McMap. All rights reserved.