public async Task <IConnectionResponse> SendAsync(object body) { logger.LogInformation("Sending request of type {0} to {1}", body.GetType(), apiName); // if we are in method read -> try to serialise to query string if (method == PrivateConnectionMethod.Read || method == PrivateConnectionMethod.Delete) { var builder = new StringBuilder(1024, 1024); using (var writer = new StringWriter(builder)) using (var logged = writer.LogWriterDebug(logger, "Generated URL {0}")) using (var urlWriter = new UrlWriter(writer)) { try { logger.LogDebug("Attempting to use Get Query"); new JsonSerializer().Serialize(urlWriter, body); writer.Flush(); queryString = BuildQuery(writer.ToString()); httpMethod = method == PrivateConnectionMethod.Read ? HttpMethod.Get : HttpMethod.Delete; return(await SendAsync(null).ConfigureAwait(false)); } catch (Exception e) { logger.LogDebug("Get query failed, using fallback", e); queryString = BuildQuery(method); httpMethod = HttpMethod.Post; } } } else { queryString = BuildQuery(); httpMethod = method == PrivateConnectionMethod.Update ? HttpMethod.Put : HttpMethod.Post; } // serialise body at this point. using (var stream = new MemoryStream()) using (var streamWriter = new StreamWriter(stream)) using (var writer = streamWriter.LogWriterDebug(logger, "Serialised Content {0}")) { JsonSerializer s = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore }; if (method == PrivateConnectionMethod.Form) { contentType = "application/x-www-form-urlencoded"; using (var urlWriter = new UrlWriter(writer) { CloseOutput = false }) { s.Serialize(urlWriter, body); } } else { contentType = "application/json"; using (var jsonWriter = new JsonTextWriter(writer) { CloseOutput = false }) { s.Serialize(jsonWriter, body); } } writer.Flush(); stream.Position = 0; return(await SendAsync(stream).ConfigureAwait(false)); } }