Hi,

I will describe what steps I need to do and what I have done. This route
servers as file uploader but not in the right sense of the word. It does not
upload file via stream but I need to send the file as part of post request

What I need to do:

1. Get QueryParams from given url e.g.
http://localhost:1555/cxf/127.0.0.1/uploadFile?port=443&user=myuser&password=mypassword&file=/path/to/myfile.txt

2. Set appropriate properties for passing data through the route

3. Pass this data to method where I set MultipartEntity and send the POST
request

--

Yesterday I was able to make the consumer work as you saw at 
http://cxf.547215.n5.nabble.com/MultipartBody-with-PathParam-and-QueryParam-td5724291.html#a5724293
<http://cxf.547215.n5.nabble.com/MultipartBody-with-PathParam-and-QueryParam-td5724291.html#a5724293>
  

The tricky part is how to send different type of data then only multipart.
For now it works with this hard-coded data:

CXF:
--------------------------------------------------------------------------------------

        @POST
        @Path("/upload")
        @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
        @Produces(MediaType.APPLICATION_JSON)
        public Response uploadFile(
                @Multipart(value = "session_id") String sessionId,
                @Multipart(value = "action") String action
        ){
                return null;
        }

JAVA:
--------------------------------------------------------------------------------------

        public void sendPost(Exchange exchange) throws IOException {

                String pathToUploader = 
"https://myserver.com/service/upload.php";;
                
                String session_id = "0kk1sobmbuforgjamj10470a23";
                String action = "A";
                File myfile = new File("/path/to/myfile.txt");
                
                HttpClient client = new DefaultHttpClient();
                
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
                HttpPost post = new HttpPost(pathToUploader);
                
                MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE );
                
                        // For File parameters
                        entity.addPart("myfile", new FileBody((( File ) myfile),
"application/octet-stream" ));
                         
                        // For usual String parameters
                        entity.addPart("session_id", new StringBody(session_id, 
"text/plain",
Charset.forName( "UTF-8" )));
                        entity.addPart("action", new StringBody(action, 
"text/plain",
Charset.forName( "UTF-8" )));
                        
                        post.setEntity( entity );                       
                        
       }

This will send the correct http request via POST method and I can see file
uploaded on my server. 

To add additional parameters I tried to do something like this:

        @POST
        @Path("/upload")
        @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
        @Produces(MediaType.APPLICATION_JSON)
        public Response uploadFile(
                @PathParam("host") String host,
                @QueryParam("port") String port,
                @Multipart(value = "session_id") String sessionId,
                @Multipart(value = "action") String action
        ){
                return null;
        }

which actually works, I can get data via MessageContentsList, set properties
and pass them to method sendPost(). 

But this is the point where sending of multipart POST request stops working.
I can see that everything is set correctly, but the request is sent with
incorrect data or format and I can't see the uploaded file.

-Br, Roman



--
View this message in context: 
http://camel.465427.n5.nabble.com/Send-file-via-POST-request-tp5728674p5728817.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to