Search code examples
vb.netrestfilebinaryresponse

tar file from restapi as application/octet-stream


I try to save a tar file from a rest request delivered as application/octet-stream

The API documentation states "Content transferred in binary (octet-stream)" but request.content doesn´t look like binary. It starts with "VW5peHRfMTY4OTA2ODgzN19TaWctMTU2OV9Mb2ctVHJhX05vLTE0OF9TdGFydF9DbGllbnQtNTU2NDYyMTUubG9nAAAAA" Thats also the start of both the saved tar files when opened with Notepad++.

Thats the short version of the REST communication and file saving

Dim request As RestRequest
request = New RestRequest("/export", Method.GET)
request.AddHeader("Accept", "application/octet-stream")
Dim response As IRestResponse
response = client.Execute(request)
Dim content As String = response.Content

If response.StatusCode = Net.HttpStatusCode.OK Then
    System.IO.File.WriteAllBytes("c:\test.tar", response.RawBytes)

'or 

    Dim stream As MemoryStream = New MemoryStream
    stream.Write(response.RawBytes, 0, response.RawBytes.Length)
    stream.Seek(0, SeekOrigin.Begin)
    Dim filestream = New FileStream($"Test1.tar", FileMode.Create)
   stream.CopyTo(filestream)
   filestream.Close()
END IF

Both give me the same file which can´t be opened with 7zip or linux because they are not recognised as archives.

The Content Type of the response is correct: "StatusCode: OK, Content-Type: application/octet-stream, Content-Length: 191832)"

Sadly there is no response.stream.

Is the error on my part or is the API sending me garbage?


Solution

  • Problem solved. The response was base64 encoded which was not specified in the documentation.

    The solution is

    System.IO.File.WriteAllBytes("c:\test.tar", Convert.FromBase64String(response.Content))