POST for ASP.NET Web API by Fiddler

I have an ASP.NET web API. I am trying to execute a POST line to an endpoint. The ASP.NET web API endpoint is as follows:

[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 the "string name" as a parameter, I can successfully execute the API endpoint. When there is a line name, I get 405 error. So, I added the following in the "Request body" section in Fiddler:

John

Unfortunately, this still causes a 405 surge. I cannot say if the web API endpoint will be configured incorrectly if I misconfigure my request in the violin. My complete request is as follows:

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 answer is as follows:

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 do not understand why POST is allowed when I do not have a parameter. However, when I add the parameter, POST does not work.

[] # :

var content = await Request.Content.ReadAsStringAsync();

, JSON content. content - .

+4
4

"":

enter image description here URL-

http://localhost:8089/api/MyApi/test

, .

- , , /. .

config.Routes.MapHttpRoute(
                name: "DefaultApi1",
                routeTemplate: "api/{controller}/{action}",
                defaults: new { action="test" }
            );

.

[HttpPost]
public async Task<IHttpActionResult> Test([Frombody]string name)
{
  int i = 0;
  i = i + 1;
  return Ok();
}
+4

{ "name" : "somtext"}
+1

.

public async Task<IHttpActionResult> Post( [ FromBody ] **object** items )
{
    Console.WriteLine( items );
    return Ok();
}
+1

Json XML, Deserialiser Asp.Net, . Json:

{ "name" : "John" }

EDIT: Your url also seems to be wrong if you use the default routes. By default, it will be selected as: the POST http://localhost:8089/api/MyApiController/"test" part is omitted because it recognizes this as a POST method. Your action should also indicate that the parameter you are accepting ("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" }
0
source

All Articles