I got a problem with a post method in java:
@POST
@Path("/test")
@Produces("application/xml")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String testing(MultipartFormDataInput input)
{
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
String name = this.getFormValue(uploadForm, "name");
List<InputPart> inputParts = uploadForm.get("file");
...
}
The listing shows the start of the POST method, which consumes a file and several other parameters like "name". Everything is working fine as long as I provide some parameters and the file as input. I also want to provide some output with the same method if there is no file provided. But in this case, I always get this error:
java.io.IOException: RESTEASY007550: Unable to get boundary for multipart
This is even the case if I manually set the content header of the request to multipart/form-data. Is there any solution to handle both use cases (params + file and only params) with one single POST method?
Thanks for the help!
Client code in python:
payload = {"name": "test"}
file = {"file": open("test.zip", "rb")}
url = "http://localhost:8080/test_war/test/test"
r = requests.post(url, data=payload, files=file)
print r.text
print r.status_code
The problem is in the client library you are using: https://github.com/kennethreitz/requests/blob/bedd9284c9646e50c10b3defdf519d4ba479e2c7/requests/models.py#L503
This line is making the assumption that if you pass files then it is a multi-part request, with no files it does something else.
Adding a fake file with a different parameter name should satisfy your server-side code:
file_name ="test.zip"
if file_name:
files = {"file": open("test.zip", "rb")}
else:
files = {"dummy_file": "nothing"}
url = "http://localhost:8080/test_war/test/test"
r = requests.post(url, data={"name": "test"}, files=files)
print r.text
print r.status_code