Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API Status Endpoint (GET and POST)

We're using New Relic to monitor many of our web applications.

This gives us availability monitoring, which allows you to specify a particular endpoint in your application that can be "pinged" to make sure you application is "alive" - obviously quite useful, but there is a small catch.

I noticed that the ping request I get from New Relic is not always a POST, it is sometimes a GET, which results in my endpoint throwing a 405 HttpStatusMessage : Method not allowed.

No problem - I figured I'd just configure my endpoint to respond to both :

[Route("status")]
public class StatusController : ApiController
{
    public IHttpActionResult Post()
    {
        return Ok();
    }

    public IHttpActionResult Get()
    {
        return Ok();
    }
}

Now granted, this does work, but seems like a lot of trouble for such a simple task.

I'm curious - is there a cleaner or better way of doing this, that I just haven't seen yet?

like image 555
X3074861X Avatar asked May 06 '26 18:05

X3074861X


1 Answers

You can get your method to accept both POST and GET

// Route /status to this controller
[RoutePrefix("status")]
public class StatusController : ApiController
{
    [HttpGet] // accept get
    [HttpPost] // accept post
    [Route("")] // route default request to this method.
    public IHttpActionResult Get()
    {
        return Ok();
    }
}
like image 67
JamesT Avatar answered May 08 '26 14:05

JamesT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!