I'm trying to send an object to an API and I got an error on it:
[HttpPost]
public async Task<object> Upsert(int Base, int SubBase,object val)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:50442/");
client.DefaultRequestHeaders.Clear();
HttpResponseMessage response = await client.PostAsJsonAsync("M01_Upsert/" + val, val);
response.EnsureSuccessStatusCode();
return response.Headers.Location;
}
}
and here is the API function I'm calling
[HttpPost]
public void M01_Upsert(object val)
{
var data = val;
}
How do I call this properly? Am I doing it correctly? It gives me a error on method to call.
Try removing the [val] that you add to the post URL.
So change the following code line:
HttpResponseMessage response = await client.PostAsJsonAsync("M01_Upsert/" + val, val);
To:
HttpResponseMessage response = await client.PostAsJsonAsync("M01_Upsert", val);
On POST requests the payload should not be a part of the URL parameters. You pass it as a parameter to the PostAsJsonAsync
method.