public JsonResult SendTest() { //We need to test this through PostMan, call GlobalPortal.Api.Demo.Controllers ->UsersController-> SendTest. Write RequestUri= //localhost/GlobalPortal.Api.Demo/Users/SendTest var user = new SynchronizationUserModel(); user.AccountId = new Guid("69194a71-64f6-4ed7-8640-6f44a31b0a61"); try { var success = SynchronizationUserProvider.CreateUser(Helpers.GetAuthenticator(), user); return(Json(success)); } catch (Exception ex) { var error = ex.GetBaseException().Message; return(Json(error)); } }
public static SynchronizationUserModel CreateUser(IServerAuthentication restClientAuthenticator, SynchronizationUserModel model) { var baseAddress = restClientAuthenticator.GetBaseAddress(); var authorizationToken = restClientAuthenticator.GetToken(); var tries = 0; var keepTrying = true; using (var restClient = new HttpClient()) { restClient.BaseAddress = new Uri(baseAddress); restClient.DefaultRequestHeaders.Accept.Clear(); restClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); while (keepTrying) { restClient.DefaultRequestHeaders.Add("Authorization", authorizationToken); var response = restClient.PostAsJsonAsync <SynchronizationUserModel>(string.Format(ApiClientGenericObjectResources.PostRequestUri, controllerName), model).Result; if (response.StatusCode == HttpStatusCode.Unauthorized) { authorizationToken = restClientAuthenticator.GetToken(true); tries++; keepTrying = tries < 4; } else { keepTrying = false; if (response.IsSuccessStatusCode) { return(response.Content.ReadAsAsync <SynchronizationUserModel>().Result); } else { var additionalInformation = response.Content.ReadAsStringAsync().Result; throw new ApplicationException(string.Format("{0}{1}", string.Format(ApiClientGenericObjectResources.UnsuccessfulResponseMessage, HttpStatusCode.BadRequest.ToString()), additionalInformation)); } } } throw new ApplicationException(string.Format(ApiClientGenericObjectResources.UnsuccessfulResponseMessage, HttpStatusCode.Unauthorized.ToString())); } }