I generated a Java API which should allow me to upload files, but it does not. Looks like there is something wrong with choosing the right content type
basePath: /
produces:
@u1982 have you tried to set a proper consumes?
I did not. Thank you, that partially solves the problem. Now I can upload files. Unfortunately, now the Content Type multipart/form-data is also set for the Parameters, which should not be the case. Is there an option to change that in the swagger.yaml? Here is what I changed in the Class ApiClient to make it work:
public Object serialize(Object obj, String contentType, Map<String, Object> formParams) throws ApiException
{
if (contentType.startsWith("multipart/form-data")) {
//...
mp.bodyPart(new FileDataBodyPart(param.getKey(),file,MediaType.APPLICATION_OCTET_STREAM_TYPE))
//originally, it is also MediaType.MULTIPART_FORM_DATA_TYPE here
} else {
mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE);
}
}
return mp;
//...
}
What do you mean by content type multipart/form-data set for parameter?j
Please share your spec via http://gist.github.com.
Here are HTTP Post like the autogenerated C# and Java clients produce. The C# Version works fine, the Java Version not. In the Java client's POST, everything what is declared in "parameters" in the swagger.yaml has the extra "Content-Type: multipart/form-data" which causes the error. In the C# client's POST the uploaded files Content-Type is application/octet-stream.
Changing the ApiClient.java like I did here forces the Java Client to produce a working HTTP POST.
For Java API client, can you try the retrofit2 API client instead to see if it has the same issue?
Here is an FAQ on how to specify library: https://github.com/swagger-api/swagger-codegen/wiki/FAQ#how-can-i-generate-an-android-sdk
Most helpful comment
Here are HTTP Post like the autogenerated C# and Java clients produce. The C# Version works fine, the Java Version not. In the Java client's POST, everything what is declared in "parameters" in the swagger.yaml has the extra "Content-Type: multipart/form-data" which causes the error. In the C# client's POST the uploaded files Content-Type is application/octet-stream.
Changing the ApiClient.java like I did here forces the Java Client to produce a working HTTP POST.