0

I am trying to test an endpoint that takes two DateTimes as parameters by sending in requests via Postman. Here is the Controller method:

[HttpGet("metrics/user/{ownerId}/{startDate}/{endDate}")]
public async Task<IActionResult> GetUserCompletionRate(int ownerId, DateTime startDate, DateTime endDate)
{
    //Check if user exists
    var userExists = await unitOfWork.userAccountRepository
            .UserAccountExists(ownerId);
    //User doesn't exist
    if (!userExists) return NotFound("Couldn't find user.");
    //User exists
    Double completionRate = await unitOfWork.completionLogEntryRepository.GetUserCompletionRate(ownerId, startDate, endDate);
    return Ok(completionRate);
}

I've tried sending them in like this:

http://.../api/metrics/user/2/2018-7-23T00:00:00/2018-7-24T10:24:00

I then tried removing the parameters from the route and send it in like this: http://.../api/metrics/user?ownerId=2&startDate=2018-7-23T00:00:00&endDate=2018-7-24T10:24:00

but nothing is working. I also tried declaring the dates in the route as {startDate:datetime} and nothing. How do I send in a DateTime object from Postman. I keep getting 404 Not Found errors.

2
  • As a starting point, I'd be inclined to try your second option, and declare them as strings, just to make sure you're getting what you expect. Once you're actually getting values, you can either just parse the strings to date time objects, or continue to figure out how to get them straight as dateTime objects. Commented Jul 25, 2018 at 6:43
  • Could you share us your full controller code? If it is in MVC Controller, did you add any Route in the Controller? For this error, it is related with the URL and router, there is no problem in DateTime. Try request URL like http://.../metrics/user/2/2018-7-23T00:00:00/2018-7-24T10:24:00. Commented Jul 25, 2018 at 8:37

1 Answer 1

1

Here are two working demo for MVC Controller and API Controller.

MVC Controller

public class HomeController : Controller
{

    [HttpGet("metrics/user/{ownerId}/{startDate}/{endDate}")]
    public async Task<IActionResult> GetUserCompletionRate(int ownerId, DateTime startDate, DateTime endDate)
    {
        return Ok("Hello");
    }

Request:https://localhost:44393/metrics/user/2/2018-7-23T00:00:00/2018-7-24T10:24:00

API Controller

[Route("api/[controller]")]
public class ValuesController : Controller
{
    [HttpGet("metrics/user/{ownerId}/{startDate}/{endDate}")]
    public async Task<IActionResult> GetUserCompletionRate(int ownerId, DateTime startDate, DateTime endDate)
    {
        return Ok("Hello");
    }

Request:https://localhost:44393/api/values/metrics/user/2/2018-7-23T00:00:00/2018-7-24T10:24:00

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.