Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a 404 response from my POST in web api?

Tags:

I have the following action in my Web api controller:

 // POST api/<controller>
    [AllowAnonymous]
    [HttpPost]
    public bool Post(string user, string password)
    {
         return true; 
    }

I am getting the following error with a 404 status when hitting it with either fiddler or a test jQuery script:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/amsi-v8.0.0/api/account'.","MessageDetail":"No action was found on the controller 'Account' that matches the request."}

My http route is as follows:

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

Gets work fine. I found another question here which talks about removing WebDAV from IIS. I tried that, still same issue.

Why do I get a 404?

like image 548
PilotBob Avatar asked Jul 26 '12 22:07

PilotBob


People also ask

How do I fix error 404 in web API?

A simple solution is to check for the HTTP status code 404 in the response. If found, you can redirect the control to a page that exists. The following code snippet illustrates how you can write the necessary code in the Configure method of the Startup class to redirect to the home page if a 404 error has occurred.

Why is my post request getting a 404?

The HTTP 404 Not Found response status code indicates that the server cannot find the requested resource. Links that lead to a 404 page are often called broken or dead links and can be subject to link rot. A 404 status code only indicates that the resource is missing: not whether the absence is temporary or permanent.

What does a 404 on API mean?

404 is a status code that tells a web user that a requested page is not available. 404 and other response status codes are part of the web's Hypertext Transfer Protocol response codes. The 404 code means that a server could not find a client-requested webpage.

Can a post return a 404?

In short: No. Your API should be "user-friendly" meaning, if there is an error it should return it in a way that the user can figure out what the problem was. Returning 404 is like saying that the service was not found which is not true.


2 Answers

The default action selection behavior in ASP.NET Web API cares about your action method parameters as well. If they are simple type objects and they are not optional, you will need to supply them in order to invoke that particular action method. In your case, you should send a request against a URI as below:

/api/account?user=Foo&password=bar

If you wanna get these values inside the request body rather than the query string (which is a better idea), just create a User object and send the request accordingly:

public class User { 
    public string Name {get;set;}
    public string Password {get;set;}
}

Request:

POST http://localhost:8181/api/account HTTP/1.1

Content-Type: application/json

Host: localhost:8181

Content-Length: 33

{"Name": "foo", "Password":"bar"}

And your action method should look like something below:

public HttpResponseMessage Post(User user) {

    //do what u need to do here

    //return back the proper response.
    //e.g: If you have created something, return back 201

    return new HttpResponseMessage(HttpStatusCode.Created);
}
like image 94
tugberk Avatar answered Sep 30 '22 17:09

tugberk


When we are posting a json it expect a class so create class in model folder like this

public class Credential
{
    public string username { get; set; }
    public string password { get;set; }
}

and now change the parameter

[HttpPost]
public bool Post(Credential credential)
{
     return true; 
}

Try now everything will work smooth

like image 34
Ali Adravi Avatar answered Sep 30 '22 18:09

Ali Adravi