I'm trying to connect to Discord's OAuth endpoint using Client credential grant
(
Found that there's a Replace which swaps out for in FormUrlEncodedContent
Ended up making my own version of ByteArrayContent/FormUrlEncodedContent
public class CustomFormUrlEncodedContent : ByteArrayContent
{
public CustomFormUrlEncodedContent(
IEnumerable<KeyValuePair<string, string>> nameValueCollection)
: base(GetContentByteArray(nameValueCollection))
{
Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
}
private static byte[] GetContentByteArray(IEnumerable<KeyValuePair<string?, string?>> nameValueCollection)
{
if (nameValueCollection == null)
{
throw new ArgumentNullException(nameof(nameValueCollection));
}
// Encode and concatenate data
StringBuilder builder = new StringBuilder();
foreach (KeyValuePair<string?, string?> pair in nameValueCollection)
{
if (builder.Length > 0)
{
builder.Append('&');
}
builder.Append(Encode(pair.Key));
builder.Append('=');
builder.Append(Encode(pair.Value));
}
return Encoding.GetEncoding(28591).GetBytes(builder.ToString());
}
private static string Encode(string? data)
{
if (string.IsNullOrEmpty(data))
{
return string.Empty;
}
// Escape spaces as ' '.
return Uri.EscapeDataString(data);//.Replace(" ", " ");
}
}