Hi
On 06/11/14 08:59, Daniel H. Peger wrote:
Hi,
this is the debug output of httpclient when uploading a simple text file only containing
the string "987654321"
DEBUG [org.apache.http.headers] http-outgoing-0 >> POST /upload?&name=test
HTTP/1.1
DEBUG [org.apache.http.headers] http-outgoing-0 >> Content-Length: 9
DEBUG [org.apache.http.headers] http-outgoing-0 >> Content-Type:
application/vnd.qlik.sense.app
DEBUG [org.apache.http.headers] http-outgoing-0 >> Host: localhost:4242
DEBUG [org.apache.http.headers] http-outgoing-0 >> Connection: Keep-Alive
DEBUG [org.apache.http.headers] http-outgoing-0 >> User-Agent:
Apache-HttpClient/4.3.5 (java 1.5)
DEBUG [org.apache.http.headers] http-outgoing-0 >> Accept-Encoding: gzip,deflate
DEBUG [org.apache.http.wire] http-outgoing-0 >> "POST /upload?name=test
HTTP/1.1[\r][\n]"
DEBUG [org.apache.http.wire] http-outgoing-0 >> "Content-Length: 9[\r][\n]"
DEBUG [org.apache.http.wire] http-outgoing-0 >> "Content-Type:
application/vnd.qlik.sense.app[\r][\n]"
DEBUG [org.apache.http.wire] http-outgoing-0 >> "Host: localhost:4242[\r][\n]"
DEBUG [org.apache.http.wire] http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
DEBUG [org.apache.http.wire] http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.3.5
(java 1.5)[\r][\n]"
DEBUG [org.apache.http.wire] http-outgoing-0 >> "Accept-Encoding:
gzip,deflate[\r][\n]"
DEBUG [org.apache.http.wire] http-outgoing-0 >> "[\r][\n]"
DEBUG [org.apache.http.wire] http-outgoing-0 >> "987654321"
As the service I try to contact is reachable only by https I can not trace the
network traffic...
Does this answer your question Sergey?
Not really; the above has no Accept header listed, but I'm not sure it
is the reason for making the request to succeed.
Do you control the target server ? What does the server logs in both cases ?
Cheers, Sergey
Best regards
Gesendet: Mittwoch, 05. November 2014 um 16:54 Uhr
Von: "Sergey Beryozkin" <[email protected]>
An: [email protected]
Betreff: Re: Upload file with CXF client proxy
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