Refit 6 makes System.Text.Json
the default JSON serializer, After I upgraded to v6.x, an exception occurred:
Refit.ApiException: An error occured deserializing the response.
---> System.InvalidOperationException: The character set provided in ContentType is invalid.
---> System.ArgumentException: 'utf8' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. (Parameter 'name')
at System.Text.EncodingTable.InternalGetCodePageFromName(String name)
at System.Text.EncodingTable.GetCodePageFromName(String name)
at System.Text.Encoding.GetEncoding(String name)
at System.Net.Http.Json.JsonContent.GetEncoding(String charset)
--- End of inner exception stack trace ---
at System.Net.Http.Json.JsonContent.GetEncoding(String charset)
at System.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsync[T](HttpContent content, JsonSerializerOptions options, CancellationToken cancellationToken)
at Refit.SystemTextJsonContentSerializer.FromHttpContentAsync[T](HttpContent content, CancellationToken cancellationToken) in /_/Refit/SystemTextJsonContentSerializer.cs:line 63
at Refit.RequestBuilderImplementation.DeserializeContentAsync[T](HttpResponseMessage resp, HttpContent content, CancellationToken cancellationToken) in /_/Refit/RequestBuilderImplementation.cs:line 324
--- End of inner exception stack trace ---
at Refit.RequestBuilderImplementation.DeserializeContentAsync[T](HttpResponseMessage resp, HttpContent content, CancellationToken cancellationToken) in /_/Refit/RequestBuilderImplementation.cs:line 330
at Refit.RequestBuilderImplementation.<>c__DisplayClass14_0`2.<<BuildCancellableTaskFuncForMethod>b__0>d.MoveNext() in /_/Refit/RequestBuilderImplementation.cs:line 277
--- End of stack trace from previous location ---
...
Response header looks like content-type: text/html; charset=utf8
I don't have permission to modify utf8
to utf-8
, and I want to use System.Text.Json
instead of Newtonsoft.Json
Use the following code to get a similar exception
Encoding.GetEncoding("utf8");
Similar issue: https://github.com/reactiveui/refit/issues/336