public static string FormatString(string content) { if (XmlUtilities.IsXml(content)) { return(XmlUtilities.TryFormatXml(content)); } else if (JsonUtilities.IsJson(content)) { return(JsonUtilities.TryFormatJson(content)); } else { return(content); } }
private static T CallRestApiWithJsonPayload <T>( this HttpClient client, string requestUri, T json, Action <string> logger, string method) { AddUserAgent(client); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); LogRequest( method, client.BaseAddress + requestUri, client.DefaultRequestHeaders, JsonConvert.SerializeObject(json, Formatting.Indented), logger); HttpResponseMessage response; switch (method.ToUpper()) { case WebRequestMethods.Http.Post: response = client.PostAsJsonAsync(requestUri, json).Result; break; case WebRequestMethods.Http.Put: response = client.PutAsJsonAsync(requestUri, json).Result; break; default: throw new InvalidOperationException(); } string content = response.EnsureSuccessStatusCode().Content.ReadAsStringAsync().Result; LogResponse( response.StatusCode.ToString(), response.Headers, JsonUtilities.TryFormatJson(content), logger); return(JsonConvert.DeserializeObject <T>(content)); }