Scott Nimrod
Scott Nimrod

Reputation: 11595

ASP.NET WebAPI unable to post

I am unable to post data to a ASP.NET WebAPI server.

I can get data from the WEBAPI server. However, I am unable to post.

The following code fails to post:

response = await client.PostAsync("api/Cars", content);

Error:

StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1

Client:

[TestClass]
public class UnitTest1
{
    // http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client
    [TestMethod]
    public async Task TestMethod1()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:48213/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // HTTP GET
            var response = await client.GetAsync("api/Cars");
            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();
            }

            // HTTP POST
            using (var content = new StringContent(@"some_value"))
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                response = await client.PostAsync("api/Cars", content);

                if (response.IsSuccessStatusCode)
                {
                    Debug.WriteLine("Post was successful.");
                }
            }
        }

WebAPI:

namespace FSharpWebAPI.Controllers

open System.Web.Http
open FSharpWebAPI.Models

type CarsController() =
    inherit ApiController()

    let values = [| { Make = "Ford"; Model = "Mustang" }; { Make = "Nissan"; Model = "Titan" } |]

    member x.Get() = values

    member x.Post(data) = 
        ignore

What updates do I need to make just to make a simple post?

Upvotes: 1

Views: 144

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233150

As @Fyodor Soikin has pointed out, the Post method is generic ('a -> 'b -> unit), and ASP.NET Web API refuses to wire up such methods.

ASP.NET Web API uses convention over configuration in order to figure out how to route and handle incoming requests. If an HTTP POST arrives, it'll go hunting for a method (partially) named Post, and attempt to call it. While you could argue that if a generic method could handle the input, it'd be possible for Web API to still call it, it doesn't do that.

You'll need to add a type annotation - for example:

member x.Post(data : Car) = 
    ignore

That's probably still not going to be enough, because the type of that version of Post is Car -> 'a -> unit, so the return value is a generic function. I'd be surprised if Web API knows what to do with that.

The reason is that ignore is a function, and since Post doesn't invoke the function, the return value is the function itself.

If you want to ignore the input and return unit, you can simply do this:

member x.Post(data : Car) = ()

This version has the type Car -> unit, which I expect Web API will find acceptable.

Upvotes: 4

Related Questions