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

Incorrect Accept header sent from Swagger-UI #485

Closed
steine-locus opened this issue Jun 27, 2014 · 21 comments · May be fixed by zhouyoufu/swagger-ui#1
Closed

Incorrect Accept header sent from Swagger-UI #485

steine-locus opened this issue Jun 27, 2014 · 21 comments · May be fixed by zhouyoufu/swagger-ui#1

Comments

@steine-locus
Copy link

I've got a java webapp where I'm doing version control via content negotiation. My method looks like:

@POST
@Consumes("application/vnd.mycompany.foo.v1+json")
@Produces("application/vnd.mycompany.bar.v1+json")
@ApiOperation(...)
@ApiResponses(...)
public Response createBar(
        @ApiParam(required = true) final Foo foo) {
    ..
}

On the UI, for Parameter Content Type, I select "vnd.mycompany.foo.v1+json". When I [Try it out!], I get back a 406 Not Acceptable. These are the headers that go out:

Accept: application/json
Content-Type: application/vnd.mycompany.foo.v1+json; charset=UTF-8

So it's not surprising that I'm getting a 406, because my response type doesn't match the accept header. I asked on the google group (https://groups.google.com/forum/#!topic/swagger-swaggersocket/NCQ3ZP7RV40), and was told I should report this as a bug.

I just grabbed the latest build from Download ZIP on the homepage and still see the issue.

@fehguy
Copy link
Contributor

fehguy commented Aug 2, 2014

Hi, this was broken. It has been fixed in swagger-js 2.0.34, and swagger-ui 2.0.19. Please try that out, and reopen if you see an issue.

@fehguy fehguy closed this as completed Aug 2, 2014
@steine-locus
Copy link
Author

I updated to the most recent versions (swagger-js 2.0.36, swagger-ui.js 2.0.21, swagger-jaxrs_2.10 1.3.7). I'm still having problems. Swagger-UI is now displaying

Response Body: no content
Response Code: 0
Response Headers: {}

I tried the query under firebug and I'm seeing this message in the console:

server can't produce application/json
swagger-ui.js (line 71)]

The headers appear to be going up correctly:

Accept  application/vnd.locustec.eim.query.secure.v1+json
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length  123
Content-Type    application/vnd.locustec.eim.query.initial.v1+json; charset=UTF-8
DNT 1
Host    localhost:8080
Referer http://localhost:8080/eim-query/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0

I see this in my Jersey log files:

14:31:46.898 [http-8080-4] DEBUG org.glassfish.jersey.tracing.general - FINISHED Response status: 200/SUCCESSFUL|OK [ ---- ms]
14:31:46.906 [http-8080-4] ERROR o.g.j.server.ServerRuntime$Responder - Error while closing the output stream in order to commit response.
java.lang.ArrayIndexOutOfBoundsException: null

When I use cURL to hit the url directly, I get a 200 and my expected data back.

@webron
Copy link
Contributor

webron commented Aug 5, 2014

@fehguy - is it possible this modification also changed swagger-ui to not assume application/json if there's no mention of consumes by default? See #518

@webron webron reopened this Aug 5, 2014
@md42
Copy link

md42 commented Aug 19, 2014

Is there any info on which version the Accept header sending is still working?

@reverb-bot
Copy link

Have you tried master? I know of no open issues for content negotiation on it

On Aug 19, 2014, at 8:04 AM, "Martin Dürrmeier" notifications@github.com wrote:

Is there any info on which version the Accept header sending is still working?


Reply to this email directly or view it on GitHub.

@fehguy
Copy link
Contributor

fehguy commented Aug 30, 2014

please use swagger-js 2.0.38 and reopen if you're having issues still.

https://raw.githubusercontent.com/wordnik/swagger-js/master/lib/swagger.js

@okigan
Copy link

okigan commented Jan 14, 2015

Seems the issue is back in :
// swagger-ui.js
// version 2.1.0-alpha.7

@webron
Copy link
Contributor

webron commented Jan 14, 2015

@okigan - can you give more information about your use case? in my test it seems to work fine.

@okigan
Copy link

okigan commented Jan 14, 2015

Ok found the issue:

@Produces({MediaType.APPLICATION_JSON, PROJECT_MODEL_JSON})
@ApiOperation(value = "Query projects")
public Iterable<Long> query

But this works:

@Produces({MediaType.APPLICATION_JSON, PROJECT_MODEL_JSON})
@ApiOperation(value = "Query projects", response = Long.class, responseContainer = "List")
public Iterable<Long> query

Would be nice if UI would show response model as list of Longs:
screen shot 2015-01-14 at 9 09 11 am

And just occurred to me, the model is different based on accept type, is that possible to specify that ?

@webron
Copy link
Contributor

webron commented Jan 14, 2015

That's not really related to the Accept header.

As for describing different models per content-type, it is currently not possible. You can follow this issue - OAI/OpenAPI-Specification#146.

@ivan-golubev
Copy link

I bumped into the same problem. Swagger-ui keeps setting the Accept:application/json.
Simple POST request with consumes: application/x-www-form-urlencoded and produces: "application/xml; charset=UTF-8".
I have tried swagger-ui-2.0.19, 2.1.0-M2.
What am I doing wrong? Is there any place in code where I can override this?

@webron
Copy link
Contributor

webron commented Apr 15, 2015

@ivan-golubev - can you share your Swagger definition?

@ivan-golubev
Copy link

OK, here is the swagger.json :

{
  "swagger": "2.0",
  "info": {
    "description": "This is a sample documentation.",
    "version": "1.0.0",
    "title": "documentation",
    "contact": {
      "email": "apiteam@wordnik.com"
    }
  },
  "host": "localhost:8080",
  "basePath": "/",
  "schemes": [
    "http"
  ],
  "paths": {
    "/users": {
      "post": {
        "summary": "Create profile",
        "description": "Authenticate user with either Facebook id and create a new profile for him.",
        "operationId": "createProfile",
        "consumes": [
          "application/x-www-form-urlencoded"
        ],
        "produces": [
          "application/xml; charset=UTF-8"
        ],
        "parameters": [
          {
            "in": "header",
            "name": "auth_method",
            "description": "facebook",
            "default": "facebook",
            "required": true,
            "type": "string"
          },
          {
            "in": "formData",
            "name": "application",
            "description": "none",
            "required": true,
            "default": "facebook",
            "type": "string"
          }
        ],
        "responses": {
          "405": {
            "description": "Invalid input"
          }
        }
      }
    }
  }
}

@webron
Copy link
Contributor

webron commented Apr 15, 2015

@ivan-golubev - there indeed seems to be an issue there. Can you please open a new ticket with the details?

@ivan-golubev
Copy link

It hasn't been resolved, but there's a walkaround available - look through
the forums here.

On Monday, August 17, 2015, teodora-yankova notifications@github.com
wrote:

Hi, has the issue with Accept always being set to application/json been
resolved? Thanks!


Reply to this email directly or view it on GitHub
#485 (comment)
.

@tokra
Copy link

tokra commented Oct 9, 2015

I having same problem: I am requesting DELETE with formData, which is requested not as api requires (MediaType.APPLICATION_FORM_URLENCODED) but UI requests MediaType.APPLICATION_JSON. This is completely wrong

@fehguy fehguy reopened this Oct 12, 2015
@fehguy
Copy link
Contributor

fehguy commented Dec 7, 2015

Verified working in master

@fehguy fehguy closed this as completed Dec 7, 2015
@tokra
Copy link

tokra commented Dec 7, 2015

Hi @fehguy, is it new SWAGGER-UI ? and also my DELETE problem mentioned above working ? thx

@fehguy
Copy link
Contributor

fehguy commented Dec 7, 2015

Yes I have just confirmed. Please try and post back.

@salluspeaks
Copy link

Actually the workaround for this to include the response value like -
@ApiOperation(value = "Query projects", response = Long.class, responseContainer = "List")
then the swagger will display the Response content type

@pruthvigh
Copy link

I am having this same issue and have been trying to resolve it from last 24 hours. Even though I specifically mentioned "consumes:["application/xml"]" still the requests go as "Accept: Application/json". Our API endpoint returns an XML response back and because of this I am getting a 0 response code and no content - response body. Our configuration is Swagger 2.0

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

Successfully merging a pull request may close this issue.

10 participants