Rest Assured does not set content length for file upload with Content-Type=application/octet-stream;
See original GitHub issueI am using Rest Assured for the first time and find it quite powerful.
I am testing one of my REST service that accepts Content-Type=application/octet-stream; and requires Content-Length header. However I can’t achieve this. The test cases fails if I explicity set the header as .header("Content-Length", fileSize)
Error:
Caused by: org.apache.http.ProtocolException: Content-Length header already present at org.apache.http.protocol.RequestContent.process(RequestContent.java:97)
If I don’t set the content length my REST service complaints. I enabled log().all() and found that the content length is not set if I use body(inputstream) or body(file). Here is the log
Request method: POST Request URI: https://localhost:8777/services/files Proxy: <none> Request params: <none> Query params: <none> Form params: <none> Path params: <none> Headers: x-mc-service-name=rewards x-file-name=TestFile.txt x-file-size=10992108 x-checksum=fa6915b728bc93dd72a679a189bd3b51 Accept=/ Content-Type=application/octet-stream; charset=ISO-8859-1 Cookies: <none> Multiparts: <none> Body: C:\Users\vb\AppData\Local\Temp\junit8893347449009337662\tmp\TestMultiLarge5MB.txt
I zeroed down the problem to this: https://github.com/rest-assured/rest-assured/blob/315d610697404209538aeb2e0f09a21ee9726006/rest-assured/src/main/java/io/restassured/internal/http/EncoderRegistry.java#L112 I am wondering why do we set content length -1 when we already know that source of content is file and can determine size of it. Any suggestions?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
I will create PR soon - just tied up at work.
I am having the same problem when I am doing a put to upload a file to Amazon S3 bucket. It needs the Content-Lenght as a header, but I can’t see it being set. I have tried what amoltambe87, but that does not seem to do anything for me. Here is what my call looks like
ValidatableResponse response = given() .header("Content-MD5", image.getMd5String64Base()) .header("Content-Type", image.getMimeType()) .contentType(image.getMimeType()) .contentType("multipart/" + image.mimeType) .header("host", "UNSIGNED-PAYLOAD") .header("charset", "UTF-8") .header("Connection", "keep-alive") .header("Cache-Control", "no-cache") .multiPart(image.fileName, image.fileName, image.imageBytes, image.mimeType) .log().all() .when() .put(url) .then() .log().body();
Any tips of how to get the Content-Length to work? Thanks!