public void SendJson() { using (TestWebHost host = new TestWebHost()) { host.StarWebHost("http://*:15001"); Thread.Sleep(1000); Uri baseUri = new Uri("http://localhost:15001"); RestApiClient client = new RestApiClient(baseUri, request => { request.Headers.Add("CustomHeader", "CustomHeaderValue"); } ); PurchaseOrder sendObj = new PurchaseOrder(); Uri relUri = new Uri(RequestPathAttribute.GetRestApiPath(sendObj), UriKind.Relative); HttpResponseMessage response = client.SendJsonRequest(HttpMethod.Post, relUri, sendObj).Result; string send = RestApiClientExtensions.GetJsonString(sendObj); string json = response.Content.ReadAsStringAsync().Result; string rest = host.Message; PurchaseOrder respObj = response.DeseriaseJsonResponse <PurchaseOrder>(); Assert.Equal(send, json); Assert.Equal(rest, json); string test = response.Headers.GetValues("CustomHeader").First(); Assert.Equal("CustomHeaderValue", test); } }
static void Main(string[] args) { Console.WriteLine("Test DotNet.RespApi.Client === "); Uri baseUri = new Uri("http://localhost:5000/"); RestApiClient client = new RestApiClient(baseUri); Uri relUri = new Uri("rest-api/WeatherForecast", UriKind.Relative); HttpResponseMessage response = client.SendJsonRequest(HttpMethod.Get, relUri, null).Result; var respStr = response.Content.ReadAsStringAsync().Result; Console.WriteLine($" ===> {relUri} ====>"); Console.WriteLine(respStr); relUri = new Uri("rest-api/market/ok", UriKind.Relative); response = client.SendJsonRequest(HttpMethod.Get, relUri, null).Result; respStr = response.Content.ReadAsStringAsync().Result; Console.WriteLine($" ===> {relUri} ====>"); Console.WriteLine(respStr); relUri = new Uri("rest-api/market/fault", UriKind.Relative); response = client.SendJsonRequest(HttpMethod.Get, relUri, null).Result; respStr = response.Content.ReadAsStringAsync().Result; Console.WriteLine($" ===> {relUri} ====>"); Console.WriteLine(respStr); var marketRequest = new TickerBitcoinRequest(); relUri = new Uri(RequestPathAttribute.GetRestApiPath(marketRequest), UriKind.Relative); response = client.SendJsonRequest(HttpMethod.Post, relUri, marketRequest).Result; respStr = response.Content.ReadAsStringAsync().Result; Console.WriteLine($" ===> {relUri} ====>"); Console.WriteLine(respStr); relUri = new Uri(RequestPathAttribute.GetRestApiPath(marketRequest) + "/fault", UriKind.Relative); response = client.SendJsonRequest(HttpMethod.Post, relUri, marketRequest).Result; respStr = response.Content.ReadAsStringAsync().Result; Console.WriteLine($" ===> {relUri} ====>"); Console.WriteLine(respStr); Console.ReadKey(); }