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.

The Content-Length header is not included in requests

See original GitHub issue

Bug Report

Description

The Content-Length header is not included in requests. I did not find out how to prevent this. Is it intentional or actually a bug? I did not find relevant documentation on this behavior of Fuel .

This keeps Fuel ⛽ from being used for APIs that respond with HTTP 411 is the field is not present. This is the case for example with the Google Play Developer API.

To Reproduce

Code to reproduce the behavior:

import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.Headers

println(Fuel
        .post("https://httpbin.org/anything")
        .header(Headers.CONTENT_LENGTH, "0")
        .responseString()
        .third
        .component1())

Output:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Java/13.0.1", 
    "X-Amzn-Trace-Id": "Root=1-XXXXXXX-XXXXXXXXXXXXXXXXX"
  }, 
  "json": null, 
  "method": "POST", 
  "origin": "--------", 
  "url": "https://httpbin.org/anything"
}

Expected behavior

A clear and concise description of what you expected to happen.

Environment

Development Machine

  • OS: Windows 10
  • Fuel version: 2.2.2
  • Kotlin version: 1.3.72 @ Java/13.0.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
felixdivocommented, Jun 9, 2020

Yes, that actually fixes it:

     private fun setBodyIfDoOutput(connection: HttpURLConnection, request: Request) {
         val body = request.body
-        if (!connection.doOutput || body.isEmpty()) {
+        if (!connection.doOutput) {
+            return
+        }
+        if (body.isEmpty()) {
+            connection.setFixedLengthStreamingMode(0L)
             return
         }

Is that change wanted/should I open a PR?

0reactions
SleeplessBytecommented, Jun 9, 2020

Yes this is great.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's the "Content-Length" field in HTTP header?
It's the number of bytes of data in the body of the request or response. The body is the part that comes after...
Read more >
Content-Length - HTTP - MDN Web Docs
The Content-Length header indicates the size of the message body, in bytes, sent to the recipient. Header type, Request header, ...
Read more >
Content-Length header is missing in POST/DELETE/PUT ...
I believe this is a bug as we're not following the RFC: A user agent SHOULD send a Content-Length in a request message...
Read more >
How do I send a POST request with Content-Length Header?
A Content-Length header is a number that indicates the size of the data in the body of the request or response in bytes....
Read more >
HTTP/1.1: HTTP Message
The presence of a message-body in a request is signaled by the inclusion of a Content-Length or Transfer-Encoding header field in the request's...
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