Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 925
Richard Jordan Avatar asked Dec 21 '12 12:12

Richard Jordan


2 Answers

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!!

like image 178
Xuwen Avatar answered Oct 18 '22 18:10

Xuwen


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.

like image 29
Vasiliy Sablin Avatar answered Oct 18 '22 18:10

Vasiliy Sablin