jim crown jim crown - 1 month ago 24
C# Question

Why I am getting a 415 response here?

I created the default Web API project using ASP.NET Core. That gave me following controller

[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}

// POST api/values
[HttpPost]
public void Post([FromBody]string value)
{
}

// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}

// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}


I am using fiddler to test the api.

When I submit a GET request
http://localhost:21674/api/values
I get a JSON object with value1 and value2 values

When I try a post to same URL, I get an unsupported media type 415 response as shown below.
Click here to see the image

What am I doing wrong?

Answer

Your request is forged wrong. There is no application\json mime-type.

The correct mime-type for JSON is application/json (notice the forward slash).

Update

Also you're not allowed to have quotes in it. The final header needs to be: Content-Type: application/json