0

I'm hoping someone can help. I have been asked to write a test application to consume a Web API.

The method I'm trying to consume, has the following signature:

[Transaction]
[HttpPost]
[Route("api2/Token/")]
public ApiToken Token(Guid companyId, DateTime dateTime, string passCode)

I've written a simple C# console application. However whatever I send to the API returns with a 404 error message and I can't see what my issue is.

My code to consume the API is as follows:

_client.BaseAddress = new Uri("http://localhost:1390");
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var companyId = Guid.Parse("4A55A43A-5D58-4245-AD7C-A72300A69865");
var apiKey = Guid.Parse("FD9AEE25-2ABC-4664-9333-B07D25ECE046");
var dateTime = DateTime.Now;

var sha256 = SHA256.Create();
var bytes = Encoding.UTF8.GetBytes(string.Format("{0}:{1:yyyyMMddHHmmss}:{2}", companyId, dateTime, apiKey));
var hash = sha256.ComputeHash(bytes);

var sb = new StringBuilder();
foreach (var b in hash)
{
    sb.Append(b.ToString());
}

try
{
    Console.WriteLine("Obtain an authorisation token");

    var response = await _client.PostAsJsonAsync("api2/Token/", new { companyId = companyId, dateTime = dateTime, passCode = sb.ToString() });

}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

All examples I've googled seem to post an object to a Web API method that accepts an object. Is it possible to post multiple simple types?

3
  • the signature of the method you called does not match the signature of the defined method, it requires a Guid, DateTime and string but you are passing an object. In order for it to receive an object, you have to define the API method as receiving an object parameter and then pass a serializabe object to it or you can just pass the parameters the way the signature has defined it. Commented Feb 24, 2017 at 10:25
  • Sorry for a simple question, but how would i pass the parameters the way the Web API signature is defined? Commented Feb 24, 2017 at 10:30
  • If you have access to the Api method declaration then, you can modify it to accept parameter as part of the route. Commented Feb 24, 2017 at 10:46

1 Answer 1

3

I don't think it's possible, from the documentation (https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api)

They've stated that

If the parameter is a "simple" type, Web API tries to get the value from the URI.

For complex types, Web API tries to read the value from the message body

You can try to use uri parameters instead

var response = await _client.PostAsJsonAsync("api2/Token/{companyId}/{dateTime}/{sb.ToString()}");

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.