Hi

What does HttpClient produces, is it actually a multipart request ?
Can you trace it and let me know what the differences are ?

Cheers, Sergey
On 05/11/14 15:35, Daniel H. Peger wrote:
Hi,
I'm trying to use CXF client proxies to connect to 3rd party REST Services 
using typed interfaces. Among other things I need to upload a file with a POST 
request to an URL. Using apache-httpclient the call to the service looks like 
this and works:

CloseableHttpClient httpclient = HttpClients.createDefault();

try {

     HttpPost httppost = new HttpPost(
             "https://localhost/upload?name=test";);

     File file = new File("d:/ws/file.txt");

     FileEntity reqEntity = new FileEntity(file,
             ContentType.create("application/vnd.qlik.sense.app"));

     httppost.setEntity(reqEntity);

     System.out.println("Executing request: "
             + httppost.getRequestLine());
     CloseableHttpResponse response = httpclient.execute(httppost);
     try {
         System.out.println("----------------------------------------");
         System.out.println(response.getStatusLine());
         System.out.println(EntityUtils.toString(response.getEntity()));
         EntityUtils.consume(response.getEntity());
     } finally {
         response.close();
     }
} finally {
     httpclient.close();
}

I tried to use the following interface to create a CXF client proxy to execute 
the call via cxf:

public interface UploadService
{
   String QUERY_PARAM_APPNAME = "name";

   @POST
   @Consumes("application/vnd.qlik.sense.app")
   @Produces("application/json")
   @Path("/upload")
   UploadResponse upload(@QueryParam(QUERY_PARAM_APPNAME) final String 
anAppName, final File anAppFile);
}

final UploadService proxy = JAXRSClientFactory.create(
                                https://localhost,
                                UploadService.class, 
Collections.singletonList(new JacksonJsonProvider()));
proxy.upload("test", new File("d:/ws/file.txt"));

Instead of using File I already tried to use InputStream or byte[] but all the 
requests result in a 400 response (BadRequest) from the server. As the server 
is a 3rd party tool I have no more information about how the file is to be 
uploaded other than that the file needs to be the request body.

I turned on the logging interceptors and the request looks fine:

Nov 05, 2014 3:42:04 PM org.apache.cxf.interceptor.LoggingOutInterceptor
INFO: Outbound Message
---------------------------
ID: 1
Address: https://localhost/upload?name=test
Http-Method: POST
Content-Type: application/vnd.qlik.sense.app
Headers: {Content-Type=[application/vnd.qlik.sense.app], 
Accept=[application/json]}
Messages: Outbound Message (saved to tmp file):
Filename: 
C:\Users\dpr\AppData\Local\Temp\2\cxf-tmp-951459\cos1238456137568485932tmp
(message truncated to 102400 bytes)

Payload: ....

But the response is

Nov 05, 2014 3:42:04 PM org.apache.cxf.interceptor.LoggingInInterceptor
INFO: Inbound Message
----------------------------
ID: 1
Response-Code: 400
Encoding: ISO-8859-1
Content-Type:
Headers: {Cache-Control=[private, must-revalidate, max-age=0], Date=[Wed, 05 Nov 
2014 14:42:03 GMT], Expires=[Wed, 05 Nov 2014 14:42:04 GMT], Server=[Repository 
API > REST Server/1.0.0.0 Microsoft-HTTPAPI/2.0], transfer-encoding=[chunked]}
Payload: File upload error

Thus my question is: How can I do the call that works using httpclient with cxf?

Best regards


Reply via email to