Search code examples
jerseyinputstreamputjersey-clienthttp-status-code-400

Jersey Client Post PUT InputStream "400 bad request"


In my code i m trying to iterate n time building a client and sending an input stream to a remote endpoint:

for (int i=0; i<=100; i ++) {                       
    Client client = Client.create();

    WebResource webResource = client.resource("https://endpoint/EndOfDayRapport/eof_" + System.currentTimeMillis());

    SPMResponse response5 = webResource
        .type("text/xml").put(SPMResponse.class,myInputStream);         
    System.out.println(response5);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The input stream contains an xml body of the message. What i noticed is that on the 1st iteration... the request succeedes 2nd iteration the response is 400 bad request... 3rd 400 bad request.... so on...

if i change the inputstream to string... taking the real xml and sending it with the PUT method. 1st iteration... the request succeedes 2nd iteration... the request succeedes 3rd iteration... the request succeedes so on...

What is the difference? why does it work with string and not with input stream? (it seems somehow the inputstream changes after the first iteration) Performance wise is it better using the inputstream or string?


Solution

  • Please check your inputstream. It seems to me that you have the following problem: On the first request your inputstream is in a valid state with data available On the following requests your stream is already at it's end. So there is nothing to read and you send empty data which the jersey handles as "bad request"

    So try to use an inputstream that you can rewind and rewind it on every loop iteration.

    But maybe that kind of defeats the whole point of using a stream anyway.