Play framework - pass multiple images in a post request
Asked Answered
R

1

0

WHAT I DID:

I am developing Rest web services using POST method in the play framework(Using Java). I did create simple web service POST API and also called it from the client side.

WHAT I WANT:

In this I want to pass multiple images as parameters in the request to the service from the client side(android/IOS/web). But I didn't get any APIs/Tutorials regarding this.

I did try to pass one image to the service. But when I pass the Image request, I am getting the null in the line "FilePart file = body.getFile("img1")".

In Application.java:

public static Result sampleMethod() {
    ObjectNode result = Json.newObject();
    result.put("message", "WS - Sample method");
    MultipartFormData body = request().body().asMultipartFormData();
    FilePart file = body.getFile("img1");
    return ok(result);
}

In routes file:

POST /sampleMethod controllers.Application.sampleMethod()

In Client.java:

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://localhost:9000/sampleMethod");
        File file = new File("<<image path>>");

        if(file.exists())
            System.out.println("File exist");
        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(file, "image/jpeg");
        mpEntity.addPart("img1", cbFile);


        httppost.setEntity(mpEntity);
        System.out.println("executing request " + httppost.getRequestLine());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();

        System.out.println(response.getStatusLine());

When I add the log "Logger.info(request().body().toString());" I am getting the below value. Is there anything problem in the request?

DefaultRequestBody(None,None,None,None,None,Some(MultipartFormData(Map(),List(),List(BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map()), BadPart(Map())),List())),false)

Has anybody done this already? Can anyone help me do this?

Retention answered 12/5, 2015 at 11:52 Comment(4)
What exactly do you need help with?Selfless
It's pretty hard to follow same question at two different posts - it would be good if you could just get the code here and close the other question. As for the code - can you check the request in debug and see, if there is a multipart parameter over there indeed.Selfless
possible duplicate of Web service call issue while passing the image - Play frameworkRohrer
@ConstantineNovykov Please help me on this https://mcmap.net/q/190134/-play-framework-websevice-activator-start-issue-when-passing-the-base64/1584121Retention
S
1

You can post multiple files and get them from request this way:

MultipartFormData body = request().body().asMultipartFormData();
FilePart picture1 = body.getFile("file1");
FilePart picture2 = body.getFile("file2");

"file1" and "file2" are the names of the post query parameters.

Speaking of the tutorials: Java file upload on Play 2.0

Selfless answered 12/5, 2015 at 11:55 Comment(9)
Thanks for the response. I did this for passing one image. But I got the null value. Can you please check this. stackoverflow.com/q/30170208/1584121Retention
Look up my comment in the question section.Selfless
I have removed the previous post and updated in this question. Can you please check my updated question and log.Retention
The log is not what you want. Please, debug the application and check the actual parameters and what they have; there seems to be a file parameter in the request though, but it needs to be debugged.Selfless
I did debug the client app and got the below values in the header. -->request POST localhost:9000/sampleMethod HTTP/1.1, -->org.apache.http.client.methods.HttpPost@60dd1773, -->Accept: application/json, -->enctype: multipart/form-data, -->accept-charset: UTF-8 and also I can see the multipart entity in the request.Retention
You have to get the request body in order to see it. You can see the multipart file in the logs, but you have to inspect, what is inside. You would be able to check the parameter name as well.Selfless
Let us continue this discussion in chat.Selfless
Sorry. Just now see the comment. Can we discuss now in the chat room?Retention
The answer given is the correct way to get the Multipart image. But I didn't receive the image in the server side. Finally I am passing the image as base64 in the parameter and receiving it in the server side.Retention

© 2022 - 2024 — McMap. All rights reserved.