ApiInvoker's invokeAPI method works great if the 'body' Object is not null. However, in case of POST method, if the body is null, the POST calls to the webserver is made without setting the Content-length header. As a result, in case of some web servers, the call fails with error 411 (content-length needed).
Note that some web servers expects content-length to be passed in the header for their POST method, else they throw error 411 (content-length required).
To fix this problem, that a lot of client code use, is to call the jersy's Builder's Post method with empty string for the second parameter(for the requestEntity parameter) instead of null. When empty string is used (instead of null) as requestEntity, the jersy client automatically sets 'content-length' to 0, satisfying all the web servers requirement of needing content-length.
Anyone see this as an issue ?? I am not able to make any post operations that do not have a body in them.
I'm curious about your use case of posting empty body and wonder if you can share more with us if you don't mind.
When posting the empty body to the server, would the server create a "default" object somehow?
The scenario that is broken is for a POST request with no body in the request.
consider 'POST http://HiService/saHi/{user_name}'. This has one PATH parameter of type string. That is it.
Now create a swagger Client Library for this operation and host your REST Api on a web server (lighttpd) that needs content-length in the header, else it will throw 411.
If you use the client library to make this call, monitor the http request (can use fiddler) and you will notice that content length is not provided. Because content length is not provided and lighttpd needs content-length for post operations, you will get 411 error.
Some articles that describes this issue are:
http://serverfault.com/questions/315849/curl-post-411-length-required
http://robertgreiner.com/2013/01/the-remote-server-returned-an-error-411-length-required/
Thanks for sharing. Would passing empty string '' instead of null to the method argument good enough as a workaround for the time being?
I think so. If requestEntity parameter is set to empty. It should solve the problem.
I believe we need to set the content type if it's posting anything other than null.
Closed via #691
Has this actually been fixed, it looks like both issues were closed citing the other?
@sjmtlewy please open a new issue with the details (e.g. HTTP library such as retrofit2, jersey2, etc) instead.
@wing328 and @sjmtlewy I noted that this problem is still open... Today I tried to build my api client with swagger-codegen-cli-2.4.7.jar and jersey2 lib... and the problema with empty body on POST call still exists
In my ApiClient.java at row 673 approximately I wrote
Entity<?> entity = serialize(body, formParams, contentType);
entity = entity.getEntity() != null ? entity : Entity.json(new byte[]{});
Response response = null;
try {
if ("GET".equals(method)) {
response = invocationBuilder.get();
} else if ("POST".equals(method)) {
response = invocationBuilder.post(entity);
} else if ("PUT".equals(method)) {
response = invocationBuilder.put(entity);
} else if ("DELETE".equals(method)) {
response = invocationBuilder.delete();
} else if ("PATCH".equals(method)) {
response = invocationBuilder.method("PATCH", entity);
} else if ("HEAD".equals(method)) {
response = invocationBuilder.head();
} else {
throw new ApiException(500, "unknown method type " + method);
}
and worked.