I'm working on a play framework 2 application and I would like to call a WebService and send a file ( an image ). I found the WS class but I cannot find how to send a file using it.
What I found is this :
WS.url("http://localhost:9001/post").post("content")
But I did not managed to send a file using a POST request.
Can someone tell me how to do it ?
Thanks.
C.C.
With Play > 2.0 this should do the trick:
File file = new File("yourPath");
WS.url("/post/url").post(file);
Being able to add a parameter identifying your file the request should be send using multipart/form-data.
This Post shows how to do it with Play! - https://stackoverflow.com/a/18723326/2788883
use following method to return response as file
RenderBinary(java.io.File file, java.lang.String name)
To see API follow this link.
Related
I am currently facing the following issue with a Java program.
I am trying to upload a file I downloaded from an external server (file type can be different for every execution) to a IBM RTC (or Jazz) server through REST call.
The REST endpoint I am sending the first request (i.e.: the file upload to the central repository) is as the following:
https://jazz.host.com:1234/ccm/service/com.ibm.team.workitem.service.internal.rest.IAttachmentRestService/?projectId=<TEAM_AREA_ID>&multiple=true&category=<CATEGORY_ID>
And this step works, uploading the file to the central repo. What it is not working is the fact that when the attachment name contains some kind of characters (i.e.: £, ù, °, ...) those are changed into the character ?.
I am using Apache HttpClient with a CloseableHttpClient, preparing the HttpEntity to be sent as follows:
java.io.File currentAttachmentFile = utils.getFileFromPath(attachmentPath);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", currentAttachmentFile, ContentType.MULTIPART_FORM_DATA, currentAttachmentFile.getName());
return builder.build();
But even sepcifying the withCharset(StandardCharsets.UTF8) to ContentType.MULTIPART_FORM_DATA does not do the trick, the file name stile contains the ? character (while I expect it to be something like: 'file_name 1!==£$%&^+ù°##'.png').
I took a look around for other suggestions but neither those found here and here are working for the file name. Moreover, specifying the Content-Type to the POST call I am sending causes a failure with the message 'Invalid POST request message' returned by RTC, so this can't be an option.
Through PostMan, the upload with the same file to the same endpoint works so I suppose there is something I am missing when creating the entity to send to RTC.
Does anyone know how to solve this?
Best regards,
Alberto
I searched a lot for methods to send GET request with JSON body.. I know it doesn't go with the GET request specs but I'm required to do that anyway.
I managed to send the request with body using Curl, but now the body is not recognized by the play application.
I want to ask if I can do this using the play framework.
Thanks.
I want to send a xml file through POST request to a HTTPS server.
I think i have to read the content of the XML file and save it to a String, then send POST it to the server.
Sorry, but i don't have any code to demonstrate my workings. Can someone help me to do a Sample code or tutorial to start with this task ?
Note : I am using Java
Using java, You can open this XML file in READ mode and start reading and printing contents in browser.
I am writing a Java client that needs to upload a file to a server using a REST post. I have a schema for information that is to be sent along with the file, but the file itself is to be sent as an Attachment to the message. The server is not written in Java and I don't have (easy) access to the source.
How do I go about creating the post message in CXF? I found a similar SO question but it seems to use Jersey-specific classes that I can't find in CXF. CXF is already being used in the project so I'd prefer to use it, but I could use another library if required.
In case it's not obvious this is the first time I've worked with a REST service.
Did you see the bit in the Apache CXF User Guide wherein WebClient is used with MultipartBody, Attachment or File? The example code excerpt is shamelessly copied below:
WebClient client = WebClient.create("http://books");
client.type("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg");
Attachment att = new Attachment("root", imageInputStream, cd);
client.post(new MultipartBody(att));
// or just post the attachment if it's a single part request only
client.post(att);
// or just use a file
client.post(getClass().getResource("image.png").getFile());
I have read plenty of explanations about how to upload files to a server through XML using C#, but I have not found how to do the same using Java and have not been able to figure it out at all. I wonder if anybody on you have done this job successfully.
My intention is to do an HTTP posts containing some text values as a caption and description of an image file, then post them and the image file to the web server through XML.
It's very easy to use Jersey to upload files. Actually, it will enable you to access file content as a InputStream. See sample code below:
#Consumes("multipart/form-data")
#POST
public void post(#FormParam("file") InputStream file) {
...
}
For client side, Jersey can also help you do send HTTP request. A good sample can be found here: http://www.tuple23.com/2010/03/file-upload-using-jersey-client.html