Search code examples
c#postf#gzipinfluxdb

POST gzip to influxdb results in "unexpected EOF"


Trying to write a gzip post request to influxdb, but getting "unexpected EOF"

The following code is in F#, but it is straightforward. You can translate it easily in your mind to C#

let gzip(s: string) =
    let bytes = Encoding.UTF8.GetBytes s
    use ms = new MemoryStream()
    use gz = new GZipStream(ms, CompressionLevel.Fastest)
    (gz.Write(bytes, 0, bytes.Length))
    ms.ToArray()
let toGzipContent(s: string) =
    let content = new ByteArrayContent(gzip s)
    content.Headers.ContentEncoding.Add "gzip"
    content.Headers.ContentType <- MediaTypeHeaderValue("text/plain", CharSet = "utf-8")
    content
let postTest() =
    let client = new HttpClient()

    let hash = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{cfg.user}:{cfg.password}"))
    client.DefaultRequestHeaders.Authorization <- AuthenticationHeaderValue("Basic", hash )
    client.DefaultRequestHeaders.ConnectionClose <- false

    let content =
        "d1fcd8ad-1dea-4a91-b1ed-eafffd497ade,tag=tag0 field=0.5177723544266878 1626025134320"
        |> toGzipContent
    let resp = client.PostAsync(cfg.url, content)
    resp

The influxdb response:

  StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Request-Id: a1edc900-e26e-11eb-b78b-244bfe8c4492
  X-Influxdb-Build: OSS
  X-Influxdb-Error: unexpected EOF
  X-Influxdb-Version: 1.8.6
  X-Request-ID: a1edc900-e26e-11eb-b78b-244bfe8c4492
  Date: Sun, 11 Jul 2021 17:37:15 GMT
  Content-Type: application/json
  Content-Length: 27
}

Sending plain text works, but gzipped not. What could be wrong here? Already looked into influxdb .NET client implementations and found no issues


Solution

  • The gzipped content is empty, the stream needs to be flushed. Inserting gz.Flush() between writing and converting the underlying ms.ToArray() should fix this.