Home > Software design >  Request ended prematurely using own Request
Request ended prematurely using own Request

Time:04-14

I'm using the packages "FastEndpoints" & "FastEndpoints.Security" for creating the RESTApi. This is my Endpoint:

public class LoginEndpoint : Endpoint<LoginRequest, LoginResponse>
{
    IUserService _userSvc;

    public LoginEndpoint(IUserService users)
    {
        _userSvc = users;
    }

    public override void Configure()
    {
        Verbs(Http.GET);
        Routes("/api/login");
        AllowAnonymous();
    }

    public override async Task HandleAsync(LoginRequest req, CancellationToken ct)
    {
        if (_userSvc.LoginValidByName(req.Name, req.Password))
        {
            var user = _userSvc.GetFromName(req.Name);
            var expiresAt = DateTime.UtcNow.AddDays(1);
            var token = JWTBearer.CreateToken(
                GlobalSettings.TOKEN_SIGNING_KEY,
                expiresAt,
                user.Permissions.Select(p => p.Name));

            await SendAsync(
                new LoginResponse()
                {
                    Token = token,
                    ExpiresAt = expiresAt
                });
        }
        else
        {
            await SendUnauthorizedAsync();
        }
    }
}

Using Postman, the endpoints works as expected: Postman Api Call

But when using RestSharp (and mind you, I'm very new to the whole RESTApi world), I get an error 'Request ended prematurely'.
This is my simple call:

public class ApiClient
{
    private RestClient _restClient;

    public ApiClient(string baseUrl)
    {
        _restClient = new RestClient(baseUrl);
        //ServicePointManager.ServerCertificateValidationCallback  = (s, c, ch, p) => true;
    }

    public async Task<bool> UserValid(string username, string password)
    {
        var request = new RestRequest("/api/login", Method.Get);
        request.AddParameter("name", username);
        request.AddParameter("password", password);
        var result = await _restClient.GetAsync(request);
        if (result.StatusCode == HttpStatusCode.OK)
            return true;
        else
            return false;
    }
}

Can someone fill me in?
Since it works with Postman, I suspect my call being bad.

CodePudding user response:

Is:

_userSvc.LoginValidByName

Or any other function missing an await by chance?

  • Related