0

I am trying to use Refit but am having an issue it says its cant find my authenticate method when trying to login a user form the client side. Refit is a strongly typed httpclient replacement. Im wondering is it cause I'm using the FromBody attribute in the swagger side?

Base url is stored in a var

public const string APIUrl = "https://localhost:44315/api";
public interface ILoginAPI
{
    [Post("/Users/")]
    Task<Token> Authenticate(User user);
}

My user Controller has the function defined as

[AllowAnonymous]
[HttpPost("Authenticate")]
public IActionResult Authenticate([FromBody] AuthenticateRequest model) {
        var response = _userService.Authenticate(model, ipAddress());
        if (response == null)
            return BadRequest(new { message = "Username or password is incorrect" });
        setTokenCookie(response.JwtToken);

        return Ok(response);
}

My Token is here as a class

public class Token
{
    public bool Authenticated { get; set; }
    public string Created { get; set; }
    public string Expiration { get; set; }
    public string AccessToken { get; set; }
    public string Message { get; set; }
}

And here is me trying to consume it its a web api project that has jwt berrer token and that functionality works as it stands.

public IActionResult Index()
{
     var loginAPI = RestService.For<ILoginAPI>(Constants.APIUrl);
        Token token = loginAPI.Authenticate(
            new User()
            {
                Username = "David",
                Password = "Test12345",
            }).Result;
     
        var mytokenHere = JsonConvert.SerializeObject(token);
        var test = token.Authenticated;
        
        return View();
}

Cause when I look at swagger ui all it shows me is username and passsword here which works.

enter image description here

Results

enter image description here

But why when I am attempting to do it thru refit is it not finding the api call.

c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100

1 Answers1

0

You have to decorate the parameter you want to use as the body of your request with [Body] attribute

public const string APIUrl = "https://localhost:44315/api";
public interface ILoginAPI
{
    [Post("Authenticate")]
    Task<Token> Authenticate([Body]User user); // => parameter decorated with [Body]
}

you can look at the documentation here for more info

Aman B
  • 2,276
  • 1
  • 18
  • 26
  • Still getting this thanks but its not helped System.AggregateException: 'One or more errors occurred. (Response status code does not indicate success: 404 (Not Found).)' – c-sharp-and-swiftui-devni Sep 13 '21 at 08:39
  • @csharpdude77 wondering if its because the path is set as `/Users/` instead of `Authenticate` – Aman B Sep 14 '21 at 09:55