Reputation: 11131
I have an ASP.NET Web API. I am trying to POST a string to an endpoint. My ASP.NET Web API endpoint looks like the following:
[HttpPost]
public async Task<IHttpActionResult> Test(string name)
{
int i = 0;
i = i + 1;
return Ok();
}
In Fiddler, I execute the following request from the composer:
POST http://localhost:8089/api/MyApiController/test
If I remove "string name" as the parameter, I can successfully execute my API endpoint. When string name is there, I get a 405 error. So, I added the following in the "Request Body" section in Fiddler:
John
Unfortunately, that still causes a 405 to be thrown. I can't tell if I'm setting up my Web API endpoint wrong if I'm setting up my request in fiddler incorrectly. My full request looks like this:
POST http://localhost:8089/api/MyApiController/test HTTP/1.1
User-Agent: Fiddler
Host: localhost:8089
Content-Length: 26
Content-Type: application/json; charset=utf-8
{ "name" : "John"}
The response looks like this:
HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache
Pragma: no-cache
Allow: GET
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-SourceFiles: =?UTF-8?B?QzpcRWNvZmljXFNvbGlkUVxKTExcamxsLW1hcmtldHNwaGVyZVxXZWJzaXRlXGFwaVxTZWFyY2hBcGlcaW5kZXg=?=
X-Powered-By: ASP.NET
Date: Fri, 12 Dec 2014 15:51:50 GMT
Content-Length: 73
{"Message":"The requested resource does not support http method 'POST'."}
I don't understand why POST is allowed when I do not have a parameter. Yet, when I add a parameter, POST does not work.
[Update] I added the following in my C# code:
var content = await Request.Content.ReadAsStringAsync();
I figured the JSON would be in the content
. However, content
is just an empty string.
Upvotes: 4
Views: 9559
Reputation: 17485
Under the "Composer" tab:
I suggest you should try with following url
http://localhost:8089/api/MyApi/test
I think that problem is with route.
You should have some route like following as default route just have controller/id. This route comes before default route.
config.Routes.MapHttpRoute(
name: "DefaultApi1",
routeTemplate: "api/{controller}/{action}",
defaults: new { action="test" }
);
Also you action should look like this.
[HttpPost]
public async Task<IHttpActionResult> Test([Frombody]string name)
{
int i = 0;
i = i + 1;
return Ok();
}
Upvotes: 4
Reputation: 11
Below, I modified the datatype of the parameter from string to object, and had success.
public async Task<IHttpActionResult> Post( [ FromBody ] **object** items )
{
Console.WriteLine( items );
return Ok();
}
Upvotes: 1
Reputation: 1806
In the body you need to add Json or XML syntax, not just then name for The Asp.Net Deserialiser to do its thing. Json would be:
{ "name" : "John" }
EDIT: Your url route also seems incorrect, if you are using the default routes. By default it will be picked up as: POST http://localhost:8089/api/MyApiController/
the "test" part is omitted as it recognises this as a POST method. Your action should also specify that the parameter you take in ("name") is expected from the body public async Task<IHttpActionResult> Test([FromBody]string name)
. Now your url will match if you do something like:
`POST http://localhost:8089/api/MyApiController/`
User-Agent: Fiddler
Host: localhost:8089
Content-Length: 26
{ "name" : "John" }
Upvotes: 0