user1099679
user1099679

Reputation: 11

How to post an XML file using a Jersey Client?

    final WebResource service = client.resource(UriBuilder.fromUri(WSURI).build());
    service.type(MediaType.APPLICATION_XML);
    service.accept(MediaType.TEXT_PLAIN);
    final Builder builder = service.header(HttpHeaders.AUTHORIZATION, HEADER);

    File file = new File("/test.xml");
    builder.entity(file);
    final ClientResponse response = builder.post(ClientResponse.class);

I want to send an XML file and receive the response back. The code that I am trying gives 400 BAD request, please could someone help. I am not sure what is going wrong here.

Upvotes: 1

Views: 2468

Answers (1)

Martin Matula
Martin Matula

Reputation: 7989

WebResource is immutable - it's methods return a new builder instance. So, the 2nd and 3rd line of your code snippet have no effect, since you ignore their result. Same when you call the entity() method. You should do the following instead:

final WebResource service = client.resource(UriBuilder.fromUri(WSURI).build());
Builder builder = service.type(MediaType.APPLICATION_XML);
builder = builder.accept(MediaType.TEXT_PLAIN);
builder = builder.header(HttpHeaders.AUTHORIZATION, HEADER);

File file = new File("/test.xml");
builder = builder.entity(file);
final ClientResponse response = builder.post(ClientResponse.class);

Upvotes: 2

Related Questions