HttpClient passes some simple parameters

I am trying to do a POST from one controller to another controller . Both controller are different projects. One project is used to simulate the level of presentation (which I will call here a test project).

From a test project, I am trying to pass 2 simple string parameters to another controller, which I will call a process.

 var values = new List<KeyValuePair<string, string>>(); values.Add(new KeyValuePair<string, string>("id", param.Id.Value)); values.Add(new KeyValuePair<string, string>("type", param.Type.Value)); var content = new FormUrlEncodedContent(values); using (var client = new HttpClient()) { client.BaseAddress = new Uri(url); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("nl-NL")); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); string token = param.token.Value; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var response = client.PostAsync("/api/Process/Product", content).Result; if (response.IsSuccessStatusCode) { var result = response.Content.ReadAsStringAsync().Result; return Request.CreateResponse(HttpStatusCode.OK, result); } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "fail"); } 

And in the process controller, I try to get it like this:

 [HttpPost] public HttpResponseMessage Product(string id, string type) { return null; } 

But he never reaches this controller . I always get a β€œstatus code not found”.

So, how can I pass 2 simple parameters using HttpClient() ?

+6
source share
4 answers

Use Get instead of Post for simple type parameters.

  using (var client = new HttpClient()) { BaseAddress = new Uri(url); client.BaseAddress = new Uri(url); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("nl-NL")); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); string token = param.token.Value; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); // New code: var response = await client.GetAsync( string.format("api/products/id={0}&type={1}",param.Id.Value,param.Id.Type) ); if (response.IsSuccessStatusCode) { var result = response.Content.ReadAsStringAsync().Result; return Request.CreateResponse(HttpStatusCode.OK, result); } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "fail"); } 

In the API side, you can do this.

 [HttpGet] public HttpResponseMessage Product(string id, string type) { return null; } 
+7
source

I'm not sure I am completely in love with this, but I used anonymous types and dynamics to handle this in the past ... (Note the differences in the settings for using PostAsJsonAsync (). I forgot about it initially.)

 client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.PostAsJsonAsync("api/User/UpdateLastLogin", new { UserId = userId, ApplicationId = applicationId }); 

Receiving Controller:

  [HttpPost] public void UpdateLastLogin([FromBody]dynamic model) { _userRepository.UpdateLastLogin((int)model.UserId, (int)model.ApplicationId); } 

In WebApiConfig.Register ():

 var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; config.Formatters.Remove(config.Formatters.XmlFormatter); 

Another approach I have passed is to create an entirely new set of strongly type models for each of these calls. I did not want to, because I had a lot of them to do this in WebAPI.

0
source

When receiving a message, you must specify [FromBody] in the parameters for the method that will be called

 [HttpPost] public HttpResponseMessage Product([FromBody]string id, [FromBody]string type) { return null; } 
0
source

Here is another example you can use for WinForms , WPF or Console apps

Client code

 async Task<CalendarView> GetData(int month, int year, int deviceTypeID) { var result = new MSOCommon.CalendarView(); try { HttpClient client = new HttpClient(); var calendarRequest = new CalendarRequest() { Month = month, Year = year, DeviceTypeID = deviceTypeID, UserInfo = Program.UserInfo }; var url = Properties.Settings.Default.ServerBaseUrl + string.Format("/api/calendar/Calendar"); HttpResponseMessage response = await client.PostAsync(url, calendarRequest.AsJson()); if (response.IsSuccessStatusCode) // Check the response StatusCode { var serSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }; string responseBody = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject<MSOCommon.CalendarView>(responseBody, serSettings); } else { logger.Error(Properties.Resources.DATACannotGetCalendar); } } catch (Exception ex) { logger.Error(Properties.Resources.DATACannotGetCalendar + " " + ex.Message); logger.Error(ex); } return result; } 

Controller Server Code

  [HttpPost()] public CalendarView Calendar(CalendarRequest calendarRequest) { logger.Info(string.Format("Get calendar for month {0} and year {1} ", calendarRequest.Month, calendarRequest.Year)); // TODO Check username var result = new CalendarView(); using (var db = new MSOnlineEntities()) { result = db.Calendars.Include("CalendarDetails") .Where(x => x.CMonth == calendarRequest.Month && x.CYear == calendarRequest.Year && x.CDeviceTypeID == calendarRequest.DeviceTypeID).ToList() .ConvertAll(x => new CalendarView { ID = x.ID, CMonth = x.CMonth, CYear = x.CYear, CDays = x.CDays, CDeviceTypeID = x.CDeviceTypeID, ClosedAtTime = x.ClosedAtTime, ClosedByUser = x.ClosedByUser, IsClosed = x.IsClosed, CalendarDetails = x.CalendarDetails.ToList().ConvertAll(d => new CalendarDetailView { ID = d.ID, CalendarID = d.CalendarID, MachineID = d.MachineID, MachineName = d.DATA_MACHINE.Name, D1 = d.D1 ?? -1, D2 = d.D2 ?? -1, D3 = d.D3 ?? -1, D4 = d.D4 ?? -1, D5 = d.D5 ?? -1, D6 = d.D6 ?? -1, D7 = d.D7 ?? -1, D8 = d.D8 ?? -1, D9 = d.D9 ?? -1, D10 = d.D10 ?? -1, D11 = d.D11 ?? -1, D12 = d.D12 ?? -1, D13 = d.D13 ?? -1, D14 = d.D14 ?? -1, D15 = d.D15 ?? -1, D16 = d.D16 ?? -1, D17 = d.D17 ?? -1, D18 = d.D18 ?? -1, D19 = d.D19 ?? -1, D20 = d.D20 ?? -1, D21 = d.D21 ?? -1, D22 = d.D22 ?? -1, D23 = d.D23 ?? -1, D24 = d.D24 ?? -1, D25 = d.D25 ?? -1, D26 = d.D26 ?? -1, D27 = d.D27 ?? -1, D28 = d.D28 ?? -1, D29 = d.D29 ?? -1, D30 = d.D30 ?? -1, D31 = d.D31 ?? -1 }) }).FirstOrDefault(); return result; } } 
0
source

Source: https://habr.com/ru/post/976823/


All Articles