I try to call my API in a method but i get the error: {StatusCode: 415, ReasonPhrase: 'Unsupported Media Type'. I have been looking around and found a lot of people have had the same problem but it have been solved by adding the media type when creating the StringContent. I have set the string content and i still get the error.
Here is my method where i try to call the API:
[HttpGet]
public async Task<ActionResult> TimeBooked(DateTime date, int startTime, int endTime, int id)
{
var bookingTable = new BookingTableEntity
{
BookingSystemId = id,
Date = date,
StartTime = startTime,
EndTime = endTime
};
await Task.Run(() => AddBooking(bookingTable));
var url = "http://localhost:60295/api/getsuggestions/";
using (var client = new HttpClient())
{
var content = new StringContent(JsonConvert.SerializeObject(bookingTable), Encoding.UTF8, "application/json");
var response = await client.GetAsync(string.Format(url, content));
string result = await response.Content.ReadAsStringAsync();
var timeBookedModel = JsonConvert.DeserializeObject<TimeBookedModel>(result);
if (response.IsSuccessStatusCode)
{
return View(timeBookedModel);
}
}
And my API method:
[HttpGet]
[Route ("api/getsuggestions/")]
public async Task<IHttpActionResult> GetSuggestions(BookingTableEntity bookingTable)
{
//code
}
I have been using the same code to call my other method and it has been working fine except in this case. I don't understand the difference between them.
Here is an example where i use basicly the same code and it works.
[HttpGet]
public async Task<ActionResult> ChoosenCity(string city)
{
try
{
if (ModelState.IsValid)
{
var url = "http://localhost:60295/api/getbookingsystemsfromcity/" + city;
using (var client = new HttpClient())
{
var content = new StringContent(JsonConvert.SerializeObject(city), Encoding.UTF8, "application/json");
var response = await client.GetAsync(string.Format(url, content));
string result = await response.Content.ReadAsStringAsync();
var bookingSystems = JsonConvert.DeserializeObject<List<BookingSystemEntity>>(result);
var sortedList = await SortListByServiceType(bookingSystems);
if (response.IsSuccessStatusCode)
{
return View(sortedList);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return RedirectToAction("AllServices");
}
And the API:
[HttpGet]
[Route("api/getbookingsystemsfromcity/{city}")]
public async Task<IHttpActionResult> GetBookingSystemsFromCity(string city)
{
//code
}