I have a Windows Phone 8 client that makes the following mail request:
public async Task<string> DoPostRequestAsync(String URI, JSonWriter jsonObject, ObservableCollection<byte[]> attachments) { var client = new RestClient(DefaultUri); var request = new RestRequest(URI, Method.POST); request.AddParameter("application/json; charset=utf-8", jsonObject.ToString(), ParameterType.RequestBody);
From the RestSharp documentation, I read that adding files to the request, it is automatically created as a "multipart / form-data" request.
The controller for the download operation in Play 2.1 is as follows:
@BodyParser.Of(BodyParser.Json.class) public static Result createMessage() { JsonNode json = request().body().asJson(); ObjectNode result = Json.newObject(); String userId = json.findPath("userId").getTextValue(); String rayz = json.findPath("message").getTextValue(); Http.MultipartFormData body = request().body().asMultipartFormData(); Http.MultipartFormData.FilePart picture = body.getFile("picture"); if (picture != null) { String fileName = picture.getFilename(); String contentType = picture.getContentType(); File file = picture.getFile(); result.put("status", "success"); result.put("message", "Created message!"); return badRequest(result); } else { result.put("status", "error"); result.put("message", "Message cannot be created!"); return badRequest(result); } }
Note that on application.conf I set the following to increase the size limit (seems to not work):
# Application settings
Now, every time I try to make a POST request, which I noticed in the debugger, the IsMaxSizeEsceeded variable is always true and that the multipart variable is null. When I tried to load one nu file using the following controller, everything seemed to work fine. Size was not a problem, and a multipart variable was set.
public static Result singleUpload() { ObjectNode result = Json.newObject(); Http.MultipartFormData body = request().body().asMultipartFormData(); Http.MultipartFormData.FilePart picture = body.getFile("picture"); if (picture != null) { String fileName = picture.getFilename(); String contentType = picture.getContentType(); File file = picture.getFile(); result.put("status", "success"); result.put("message", "File uploaded!"); return badRequest(result); } else { result.put("status", "error"); result.put("message", "File cannot be uploaded!"); return badRequest(result); } }
The problem is that attachments / files must be sent / uploaded together with the JSON object to the server in one POST request and NOT separately.
Has anyone encountered similar problems before? Is it possible to achieve this: send a json object and several files that will be uploaded to the server in the same POST request with Play 2.1?