question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rest Assured does not set content length for file upload with Content-Type=application/octet-stream;

See original GitHub issue

I 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:open
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
vjykumarcommented, May 7, 2018

I will create PR soon - just tied up at work.

0reactions
nrstabercommented, Apr 29, 2020

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to send Content-Length in rest assure ,Since ...
and due to that, I am encountering the error that "Content-Length header already present". it would be great if someone can help me...
Read more >
[rest-assured] Content-Length incorrect when using streams?
With rest-assured the '413' is returned only after the file completes upload which can take a while when the file is 6Gb. When...
Read more >
How to build a file upload service with vanilla JavaScript
Build a file upload service with vanilla JavaScript from scratch with a method that requires no npms and no dependencies.
Read more >
Vector Assets API - LinkedIn - Microsoft Learn
Partners can create and upload rich and engaging personal or organization content to be surfaced ... BLOCKED The content must not be served....
Read more >
HTTP API V2 - Docker Documentation
The header Docker-Content-Digest should not be trusted over the “local” ... PUT /v2/<name>/blobs/uploads/<uuid>?digest=<digest> Content-Length: <size of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found