0

I have an ASP.NET Web API REST Service. It has been build using standard ASP.NET Framework 4.5 and Web API 2.2 (version 5.2.3) on Visual Studio 2013 IDE.

Using Fiddler tool I am trying to check the api methods but I get http error 405. Below some screenshots from Fiddler:

Headers tab information:

enter image description here

Raw tab information: enter image description here

Basically it says:

The requested resource does not support http method 'POST'."

Below some code snippet from my ASP.NET Web API REST Service.

WebApiConfig.cs :

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "WarehouseApi",
            routeTemplate: "api/{controller}/{action}/{data}"
        );
    }
}

Controller:

[RoutePrefix("api/Warehouse")]
public class WarehouseController : ApiController
{ 
    public HttpResponseMessage GetOkResponse()
    {
         return Request.CreateResponse(HttpStatusCode.OK);            
    }

    [HttpPost]
    [Route("DumpIntoFile/{data}")]                
    public HttpResponseMessage DumpIntoFile(string data)
    {
           // Stuff here
    }
}

To test it using Fiddler, from the composer I do:

enter image description here

However If I call GetOkResponse action method from Fiddler using GET method and url: wstestDumpFile.my.domain/api/Warehouse then it works.

4
  • The error message is very clear : "The server won't accept a Post". Not all server are the same. Each have their own requirements. So you can't post data to a server that will not accept the data.
    – jdweng
    Commented Dec 11, 2020 at 15:21
  • try removing route attribute from the method DumpIntoFile Commented Dec 11, 2020 at 15:27
  • @jdweng How do you know if a server accepts POST messages?
    – Willy
    Commented Dec 11, 2020 at 19:12
  • Only by the documentation or you designed the server to accept POST,
    – jdweng
    Commented Dec 12, 2020 at 9:00

1 Answer 1

1

Change from this: [Route("DumpIntoFile/{data}")] to [Route("DumpIntoFile")], you are passing the data as a post and therefore are not passing the data as a route parameter. Then you can add: [FromBody] to your method parameter like: ([FromBody] string data)

5
  • If I do what you say then I get HTTP/1.1 415 Unsupported Media Type
    – Willy
    Commented Dec 11, 2020 at 15:27
  • @Ralph Try remove the quotes around the parameter name data, like so {data: "test"} in the request body. Commented Dec 11, 2020 at 15:30
  • From Fiddler If i put {data : "test" } in the request body it throws error http 415 as well but If I do not specify nothing in request body (leave it empty, I mean, I do not pass any parameter it works). Why? so how to pass the parameter string from fiddler?
    – Willy
    Commented Dec 11, 2020 at 15:40
  • In the request header I was missing Content-Type: Application/json and then in the request body I can specify { data : "hello" } and then it works.Then using your solution it works. Thanks.
    – Willy
    Commented Dec 11, 2020 at 16:05
  • @Ralph Glad to help and glad it's working for you now. Commented Dec 11, 2020 at 16:20

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.