-
Notifications
You must be signed in to change notification settings - Fork 6k
HTTP-GET header Content-Type is not set, cause server check error #2254
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
Comments
I added three lines to auto-generated ApiClient.java, then it works Old code begins:
Insert following code:
Continue old code:
|
But |
dear sir, I agree with you, but once define consumes property, the spring framework check request content-type, even HTTP GET. I can not accuse spring guys, because HTTP specification not strictly say no Content-Type for HTTP-GET, just SHOULD. In other words, Anyone can legally send HTTP-GET with Content-Type and empty body or form! I hav did the experiment use ajax or nodejs. There were many same question about Content-Type and HTTP-GET coexistence in stackoverflow. To resolve this problem, the only way: add Content-Type to HTTP-GET in client code |
The problem is : once user define "consumes" type in yaml, swagger's auto-generated java server and java client will not works. Other type such as nodejs ApiClient.js, it did set Content-Type in HTTP-GET request. Would you please consider this problem again? |
Have you tested Java API client using other HTTP library (not the default Jersey1.x)? You can do so by passing |
i have not tested jersey2, but i have chekced jersey2's ApiClient.java, same problem there. My project do not want to specify extra option to use jersey2. |
Do you mind testing other Java HTTP libraries to see if there are similar issue? If yes, we want to fix those as well |
Ok, i will try others. 2016年2月27日土曜日、[email protected]さんは書きました:
|
Sending a Content-Type where there is no body (other than in a response to HEAD) sounds wrong. RFC 7231, Section Content-Type:
(This "selected representation" is for responses to HEAD requests.) I think the top-level consumes property should certainly not apply to GET (or HEAD) requests. (And maybe there is a bug in the Swagger-generator for Spring-MVC.) |
@ePaul I understand.
So i have to define "consumes" globally, for any HTTP type. As you can see in above screenshot in first post, So, we have three choice: 1. Modify spring framework's check logic: ignore content-type check if no body.2. Modify swagger's auto-generated client logic to add Content-Type to request unconditionally.3. Modify swagger's auto-generated server to tell spring not use "consumes" when HTTP GET.4. Modify swagger's YAML spec to let "consumes" property apply to down level.How do you think? |
Think more and more, i found it seems more reasonable to do option 3(modify swagger's server code):
But i am worried about server does not work in case of someone send JSON body in request of HTTP GET. Any idea? |
@QianJin2013 Your syntax error is resolved by adding a space after the The following Yaml shows no errors in the online swagger-editor, and generates server code with proper swagger: '2.0'
info:
version: "0.0.0"
title: <enter your title>
produces:
- application/mycom-v1+json
paths:
/persons:
post:
consumes:
- application/mycom-v1+json
description: |
Gets `Person`s related to given person.
parameters:
- in: body
name: person
schema:
type: object
title: Person
properties:
name:
type: string
responses:
200:
description: Successful response
schema:
title: ArrayOfPersons
type: array
items:
title: Person
type: object
properties:
name:
type: string Generated PersonsApi.java (imports omitted): @Controller
@RequestMapping(value = "/persons", produces = {APPLICATION_JSON_VALUE})
@Api(value = "/persons", description = "the persons API")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringMVCServerCodegen", date = "2016-02-29T21:48:05.397Z")
public class PersonsApi {
@ApiOperation(value = "", notes = "Gets `Person`s related to given person.", response = InlineResponse200.class, responseContainer = "List")
@io.swagger.annotations.ApiResponses(value = {
@io.swagger.annotations.ApiResponse(code = 200, message = "Successful response") })
@RequestMapping(value = "",
produces = { "application/mycom-v1+json" },
consumes = { "application/mycom-v1+json" },
method = RequestMethod.POST)
public ResponseEntity<List<InlineResponse200>> personsPost(
@ApiParam(value = "" ) @RequestBody Person person
)
throws NotFoundException {
// do some magic!
return new ResponseEntity<List<InlineResponse200>>(HttpStatus.OK);
}
} |
I think your option 3 (ignoring the consumes for GET and other no-body methods, both for server and client generation) is also a good idea. |
@ePaul I am glad to here this news. No wonder i saw no error when i first moved consumes property down. So, i feel it's good to just move consumes property down without modifying any other files. I think this issue can be closed. |
Thanks swagger api!
I found JavaClientCodegen has a problem.
When i define consumers in yaml, it means send request with the specified Content-Type, but unfortunately, HTTP GET does not works, POST and others works.
Sample yaml:
Result ClientAPI.java
As you can see above code, contentType is not used for HTTP GET.
When it goes to a spring framework server, the problem occours:

So, the server did right thing, but client did wrong. I need client send Content-Type for HTTP GET.
Would you confirm this problem?
The text was updated successfully, but these errors were encountered: