Domanda

enter code hereI need to send a multipart request to a rest api signed with oAuth

in my pom I'm using

 <dependency>
            <groupId>oauth.signpost</groupId>
            <artifactId>signpost-commonshttp4</artifactId>
            <version>1.2.1.1</version>
 </dependency> 

and I'm using this code to add a multipart form

consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
      consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        consumer.setTokenWithSecret("XXXX", "YYYY");
        System.out.println("Fetching request token from "
                + REQUEST_TOKEN_ENDPOINT);




PostMethod filePost = new PostMethod("http://..../");               
                Part[] parts = {
                        new FilePart("metadata", temp, "application/xml", "UTF8"),
                        new FilePart("attachment", imageFile, "image/jpeg", null),                      
                };
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));

                 HttpClient client = new HttpClient(); 

                 int status = client.executeMethod(filePost);

the problem is that to do the consumer.sign(request), to sign the request with oAuth, I need to have an HttpRequest...

so my question is, what can I do to send a multipart POST request with something similar like this using an HttpRequest from apache.

Thanks

È stato utile?

Soluzione

Well I reply myself since I solved my problem. I paste here how to do it, hope it may be useful for some body

HttpPost uploadBackgroundPost = new HttpPost ("http://.../");
                 consumer.sign(uploadBackgroundPost);
                 MultipartEntity entity = new MultipartEntity (HttpMultipartMode.STRICT); 

                                                FileBody tempBody = new FileBody(temp, "application/xml"); 
                                                 FileBody imageBody = new FileBody(imageFile, "image/jpeg"); 
                                                 entity.addPart("metadata", tempBody);
                                                 entity.addPart("attachment", imageBody); 
                                                 uploadBackgroundPost.setEntity(entity); 
                                                 DefaultHttpClient httpClient = new DefaultHttpClient(); 

                                                 System.out.println(httpClient.execute(uploadBackgroundPost, new BasicResponseHandler())); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top