I am trying to create a Web API client C# console app to consume a Restful Web API class in Visual Studio. For the client, I am having issues with updating a record. I want to let the user enter an existing record ID number. One record I have in my Product table is:
Manufacturer = "Dell", OperatingSystem = "Windows 10", Name = "AMD A6-Series", Price = 279.99m, ItemsInStock = 600;
Here is the method I have so far:
static async Task RunAsyncUpdateRecord()
{
Console.Write("Enter Product ID: ");
string inputID = Console.ReadLine();
int id;
if (int.TryParse(inputID, out id))
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:61147/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.PutAsync("api/products/" + id); if (response.IsSuccessStatusCode)
{
Console.Write("Success");
}
else
{
Console.Write("Error");
}
}
}
else
{
Console.WriteLine("Input is invalid, you must enter an integer for the record number.");
}
}
I either keep getting the error message from Console.Write("Error");
or nothing happens at all.