Ismael
Ismael

Reputation: 282

Having problem with HttpClient - PostAsJsonAsync in blazor

I am trying to use the PostAsJsonAsync in the same way it used to work PostJsonAsync before been updated, but Im getting this error:

CS1503 Argument 3: cannot convert from 'ICCR.Shared.LoginModel' to 'ICCR.Shared.LoginResult' ICCR.Client

And the registerModel in the code bellow its marked in red, I would thank if you tell me how to solve it.

public async Task<RegisterResult> Register(RegisterModel registerModel)
    {
    
        var result = await _httpClient.PostAsJsonAsync<RegisterResult>("api/accounts", registerModel);
        return result;
    }

Upvotes: 6

Views: 8490

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273611

PostAsJsonAsync() returns an HttpResponseMessage.

public async Task<RegisterResult> Register(RegisterModel registerModel)
{    
   var response = await _httpClient.PostAsJsonAsync("api/accounts", registerModel);
   return await response.Content.ReadFromJsonAsync<RegisterResult>();
}

the type of the input parameter for Post() can be inferred, the return type from Read() has to be specified with <>.

Upvotes: 12

Related Questions