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.

FuelError content is empty if "Content-Type" header is not provided by a server

See original GitHub issue

Description

FuelError content is empty if Content-Type header is not provided by a server

To Reproduce

import com.github.kittinunf.fuel.core.Request
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.result.Result


suspend fun <T> Request.requestWithAuthKey(
    clazz: Class<T>,
    permanentKey: String
): Unit {


    val (_, _, fuelResult) = appendHeader("AuthKey" to permanentKey)
        .authentication()
        .basic(userName, password)
        .awaitByteArrayResponseResult()


    when (fuelResult) {
        is Result.Failure -> logError()
        else -> { /* don't care */}
    }
} 

fun Result.Failure<FuelError>.logError()

    val bytes = error.response.data // empty if Content-Type header is missing, OK, if present
    val len = error.response.contentLength; // always OK, number of response's bytes
    val body = error.response.body().asString("text/plain") // empty if Content-Type header is missing, OK, if present
}

Expected behavior

Error content should be extractable no matter whether or not response contains Content-Type header.

Development Machine

Complete the following information if applicable

Windows 10, Kotlin 1.3, Android Studio 3.6.3

Smartphone or Emulator

Xiaomi Redmi Note 8 Pro or Genymotion

Additional context

###Response headers

Date | Mon, 11 May 2020 15:44:59 GMT
-- | --
Server | Apache/2.4.35 (Win64) OpenSSL/1.1.1b PHP/7.2.19
Content-Length | bytes count here
Connection | close
Content-Type | application/json; charset=UTF-8 (or absent)

Response body

{  "testCyrillicValue": "абвгдеж"  }

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
SleeplessBytecommented, May 12, 2020

Content-Length ís a leaky abstraction. You can not use the Content-Length to determine the actual content-length, as soon as you have a chunked, gzip or br or deflate Transfer-Encoding. That’s how it’s specced up in HTTP standards. Nothing we can do about that. The double downside is that Content-Encoding is often abused as Transfer-Encoding, meaning that usually Content-Encoding also breaks the Content-Length because the server will incorrectly report the “original” size, instead of the encoded size.

Data should not be empty if there really is an error message. That is a 🐛 bug. We would love to fix that 👍 😄

1reaction
SleeplessBytecommented, May 11, 2020

If Content-Type is not given, we can not extract any content, because we don’t know what the content is. We could expose the data stream as bytes or strings or whatever, and then you can use that directly (instead of the .as(xxx)) – or perhaps allow this behaviour for string content only, because that’s generally safe.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin - Fuel - Documentation - GitBook
"Body : (empty)". * "Headers : (1)". * Content-Type : application/x-www-form-urlencoded. */. ​. // What is actually sent to the server.
Read more >
Fuel HTTP Library with Kotlin - Baeldung
A tutorial on a popular HTTP Kotlin library and its modules.
Read more >
Should Content-Type header be present when the message ...
Is the correct combination of HTTP headers in this case to have no Content-Type and a Content-Length of 0, or should the Content-Type...
Read more >
X-Content-Type-Options - HTTP - MDN Web Docs - Mozilla
The X-Content-Type-Options response HTTP header is a marker used by the server to indicate that the MIME types advertised in the ...
Read more >
https://raw.githubusercontent.com/kittinunf/fuel/m...
Therefore by default, parameter encoding is ignored by `ParameterEncoder` if the content type is `multipart/form-data`. #### `Parameters` with empty, array, ...
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