0

I've got a Xamarin application using Flurl, with the following post to a Web Api

Xamarin App:

    private async Task<LoginResponse> processLogin()
    {
        try
        {

            return await "http://192.168.0.12:60257/api/loginapi/Login".WithTimeout(10).PostJsonAsync(new { username = "fsdafsd", password = "gdfgdsf" }).ReceiveJson<LoginResponse>();
        }
        catch (Exception e)
        {
            return new LoginResponse { ResponseStatusCode = -1 };
        }
    }

Web Api:

public LoginResponse Login([FromBody]LoginRequest loginRequest)
{
    var result = new LoginResponse();

    try
    {
        var user = this.UserManager.FindAsync(loginRequest.username, loginRequest.password);

        if (user != null)
        {
            result.ResponseStatusCode = 1;
        }
        else
        {
            result.ResponseStatusCode = 0;
        }

    }
    catch (Exception e)
    {

        result.ResponseStatusCode = -1;
    }

    return result;

}

I can see my Web Api method getting hit, and it returns the expected object type, not my Xamarin application continues to wait on the Flurl Post.

Can anyone advise what I might be doing wrong?

UPDATE:

I have noticed that the following does work, but it's not ideal:

dynamic result = await "http://192.168.0.12:60257/api/loginapi/Login".PostJsonAsync(new { username = "fsdafsd", password = "gdfgdsf" }).ReceiveJson();

Martin Crawley
  • 477
  • 1
  • 7
  • 16

1 Answers1

0

Fixed it. For whatever reason, it was the type I was trying to return. Changing the object variable type to "dynamic" fixed this, and allowed me to deserialise the object correctly.

dynamic result = await "http://192.168.0.12:60257/api/loginapi/Login".PostJsonAsync(new { username = "fsdafsd", password = "gdfgdsf" }).ReceiveJson();

Returns a dynamic object with the properties I'd expect in the normal structure.

If anyone can enlighten my why I couldn't do:

LoginRequest result = ...

It'd be appreciated.

Martin Crawley
  • 477
  • 1
  • 7
  • 16