So I'm just playing around with Spotify's Web API and I'm trying to access my top played tracks. Although I've encountered a problem I've been trying to solve for a couple of hours now but I can't find an answer.
When I try to deserialize my response, I get the follwing error:
'UTF8' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. Parameter name: name The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set.
The ContentType is application/json; charset=UTF8
Any ideas?
Here's my request code:
private static HttpClient GetHttpClient()
{
HttpClientHandler handler = new HttpClientHandler() {
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
var httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
return httpClient;
}
public async Task<SearchArtistResponse> GetSelfTopAsync(string type, string userName)
{
var httpClient = GetHttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GetAccessToken(userName));
var sb = new StringBuilder();
sb.Append(ApiUrl);
sb.Append($"/me/top/{type}");
var query = sb.ToString();
var response = await httpClient.GetAsync(query);
var spotifyResponse = JsonConvert.DeserializeObject<SearchArtistResponse>(await response.Content.ReadAsStringAsync());
return spotifyResponse;
}
UTF-8 supports any unicode character, which pragmatically means any natural language (Coptic, Sinhala, Phonecian, Cherokee etc), as well as many non-spoken languages (Music notation, mathematical symbols, APL). The stated objective of the Unicode consortium is to encompass all communications.
UTF-8 is an encoding system for Unicode. It can translate any Unicode character to a matching unique binary string, and can also translate the binary string back to a Unicode character. This is the meaning of “UTF”, or “Unicode Transformation Format.”
Fortunately UTF-8 is the default per sé. When reading an XML document and writing it in another encoding, mostly this attribute will be patched too.
The Difference Between Unicode and UTF-8 Unicode is a character set. UTF-8 is encoding. Unicode is a list of characters with unique decimal numbers (code points). A = 65, B = 66, C = 67, ....
Are you using .net core?
You will need to add the following code to make the encodings available in .NET desktop available in your environment:
System.Text.EncodingProvider provider = System.Text.CodePagesEncodingProvider.Instance;
Encoding.RegisterProvider(provider);
More info on CodePagesEncodingProvider.Instance
can be found here.
The problem should be a validation of response header Content-Type
,that occur when you call ReadAsStringAsync()
, if you call ReadAsByteArrayAsync()
instead and parse to string
(System.Text.Encoding.UTF8.GetString())
that will gonna work!!!
I had a same problem while I was trying to get an answer from my API which is built in PHP using C# service. I could fix the issue by changing "charset=UTF8" to "charset=\"UTF-8\"" on the PHP side(the api that sends result to the C# service). Hope this helps someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With