I want to make an insertion from a distant client for that I need to send data via http.
I can use the getPerformances()
correctly with an httpClient
api/performances?date={0}
I want to ask if my postPorformances()
implemntation inside my PerformancesController
is corrrect and if it is how to call it from a client?
Here is my implementation:
public class PerformancesController : ApiController
{
// GET api/performances
public IEnumerable<Performance> getPerformances(DateTime date)
{
return DataProvider.Instance.getPerformances(date);
}
public HttpResponseMessage postPerformances(Performance p)
{
DataProvider.Instance.insertPerformance(p);
var response = Request.CreateResponse<Performance>(HttpStatusCode.Created, p);
return response;
}
}
public class Performance {
public int Id {get;set;}
public DateTime Date {get;set;}
public decimal Value {get;set;}
}
I have tried this one but I'm note sure:
private readonly HttpClient _client;
string request = String.Format("api/performances");
var jsonString = "{\"Date\":" + p.Date + ",\"Value\":" + p.Value + "}";
var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
var message = await _client.PutAsync(request, httpContent);