Problem with iOS and header API versioning

Stesvis 1,041 Reputation points
2021-05-30T18:53:42.56+00:00

We have implemented API versioning using headers, so in HttpClient on the mobile app code (Xamarin Forms), we do something like this:

_myHttpClient.DefaultRequestHeaders.Add(CustomRequestHeaders.ApiVersion, "2");

In the backend, we have a UsersController and a Users_v2Controller, and we need to consume the method GetUsersByRole.

UsersController:

[Authorize]
    [RoutePrefix("api/Users")]
    public class UsersController : BaseApiController
    {
        // ...

        [HttpGet]
        [Route("")]
        public IHttpActionResult GetUsersByRole([FromUri] int clientId, [FromUri] string role)
        {
            if (ApiVersion == "2")
                return RedirectToRoute("GetUsersByRole_v2", new { clientId = clientId, role = role });

                return Ok ("V1 results");
        }

        // ...

    }

Users_v2Controller:

[Authorize]
    [RoutePrefix("api/Users")]
    public class Users_v2Controller : BaseApiController
    {
        // ...

        [HttpGet]
        [Route("", Name = "GetUsersByRole_v2")]
        public IHttpActionResult GetUsersByRole([FromUri] int clientId, [FromUri] string role)
        {
                return Ok ("V2 results");
        }

        // ...

    }

Now, I am consuming the GET /api/users?clientId=1&role=Admin with ApiVersion=2 from android, iOS and Postman to double check:

  • Android: returns V2 results
  • Postman: returns V2 results
  • iOS: returns 401: unauthorized but if i set ApiVersion=1 it works and returns V1 results

What could the problem be? Why would iOS say i am unauthorized?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,332 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,445 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.