Closed emako closed 3 months ago
My temporary solution
public static async Task<T> GetJsonAsync<T>(this IFlurlRequest request, object body, CancellationToken cancellationToken = default)
{
// Not support cancellation.
_ = cancellationToken;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(request.Url);
webRequest.ContentType = "application/json";
webRequest.Method = "GET";
webRequest.PreAuthenticate = true;
webRequest.Headers.Add("Authorization", "Bearer " + AccessToken);
object currentMethod = typeof(HttpWebRequest)
.GetProperty("CurrentMethod", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(webRequest);
currentMethod.GetType()
.GetField("ContentBodyNotAllowed", BindingFlags.NonPublic | BindingFlags.Instance)
.SetValue(currentMethod, false);
using StreamWriter streamWriter = new(webRequest.GetRequestStream());
await streamWriter.WriteAsync(JsonSerializer.Serialize(body));
streamWriter.Close();
HttpWebResponse response = (HttpWebResponse)await webRequest.GetResponseAsync();
using Stream responseStream = response.GetResponseStream();
using StreamReader myStreamReader = new(responseStream, Encoding.UTF8);
string json = await myStreamReader.ReadToEndAsync();
return JsonSerializer.Deserialize<T>(json)!;
}
This support is not suitable
It's actually far easier than that - use SendJsonAsync
: https://flurl.dev/docs/fluent-http/#additional-use-cases
Although it is not good to use GET method with body, but it is technically allowed by http protocol.
And actually I need to use it.