سؤال

How do I post GZip data using RestSharp. I have the following code but it isn't working as I would expect:

var restRequest = new RestRequest(url, Method.POST)
{
    Timeout = Constants.DefaultTimeoutMilliseconds
};

var dataStream = new MemoryStream();

using (var zipStream = new GZipStream(dataStream, CompressionMode.Compress))
{
    using (var writer = new StreamWriter(zipStream))
    {
        writer.Write(new DotNetXmlSerializer().Serialize(content));
    }
}

var compressedBytes = dataStream.ToArray();

restRequest.AddParameter("application/x-gzip", compressedBytes, ParameterType.RequestBody);

return _restClient.Execute<TResponseData>(restRequest);

When I run this and check the wireshark trace, the compressedBytes variable is posted as 'System.Byte[]' - as if ToString() has been called on it despite the parameter being a system.object.

If I pass the compressed byte array through as a string using both Convert.ToBase64String() and Encoding.Utf8.GetString() then I am unable to decompress the GZip at the server. I simply get 'System.IO.InvalidDataException: The magic number in GZip header is not correct. Make sure you are passing in a GZip'.

Is there any way of posting Gzipped data using RestSharp?

هل كانت مفيدة؟

المحلول

Make sure you've updated to the latest version of RestSharp (like 104.4.0) as this was a bug in a previous version. I think this was fixed in 104.2 where the PUT or POST of binary data ended up with the System.Byte[] being represented as the string.

Update your NuGet reference and try it again. Good luck!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top