f# - ASP.NET WebAPI unable to post -
i unable post data asp.net webapi server.
i can data webapi server. however, unable post.
the following code fails 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 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 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 need make make simple post?
as @fyodor soikin has pointed out, post
method generic ('a -> 'b -> unit
), , asp.net web api refuses wire such methods.
asp.net web api uses convention on configuration in order figure out how route , handle incoming requests. if http post arrives, it'll go hunting method (partially) named post
, , attempt call it. while argue if generic method handle input, it'd possible web api still call it, doesn't that.
you'll need add type annotation - example:
member x.post(data : car) = ignore
that's still not going enough, because type of version of post
car -> 'a -> unit
, return value generic function. i'd surprised if web api knows that.
the reason ignore
function, , since post
doesn't invoke function, return value function itself.
if want ignore input , return unit
, can this:
member x.post(data : car) = ()
this version has type car -> unit
, expect web api find acceptable.
Comments
Post a Comment