playframeworkmultipartform-datacontent-lengthplayframework-webservice

Set content length header in java play framework http request


I am using play 2.5.x ws library for a file upload to an internal server. I have sample code working with apache http/curl/postman. But when I try to use ws library some how its failing. I don't have access to the internal server and its not returning any error message

I have checked the request headers from both requests(apache http/play WS) and I see that "Content length " header is missing from WS request. I tried to set it with

ws.url(url).setHeader().post(Source.from(Arrays.asList(fp,name)

but I am getting

"Stream has already been consumed and cannot be reset"

Is there any way I can set the content length ?. Or is there anything else I am missing.


Solution

  • Unfortunately, this is not properly documented, but you can look at the code to understand how post(Source) works:

    // If the body has a streaming interface it should be up to the user to provide a manual Content-Length
    // else every content would be Transfer-Encoding: chunked
    // If the Content-Length is -1 Async-Http-Client sets a Transfer-Encoding: chunked
    // If the Content-Length is great than -1 Async-Http-Client will use the correct Content-Length
    

    -1 is the default.

    So, currently, it us up to you to set Content-Length or make your server accept chunked transfer.