2

I'm working on making a POST request using my Xamarin form to send data to an Action in my controller in my WebAPI project. The code with breakpoints doesn't go beyond

client.BaseAddress = new Uri("192.168.79.119:10000");

I have namespace System.Net.Http and using System mentioned in the code.

 private void BtnSubmitClicked(object sender, EventArgs eventArgs)
    {
        System.Threading.Tasks.Task<HttpResponseMessage> statCode = ResetPassword();
        App.Log(string.Format("Status Code", statCode));


    }
    public async Task<HttpResponseMessage> ResetPassword()
    {
        ForgotPassword model = new ForgotPassword();
        model.Email = Email.Text;
        var client = new HttpClient();

        client.BaseAddress = new Uri("192.168.79.119:10000");

        var content = new StringContent(
           JsonConvert.SerializeObject(new { Email = Email.Text }));

        HttpResponseMessage response = await client.PostAsync("/api/api/Account/PasswordReset", content); //the Address is correct

        return response;
    }

Need a way to make a Post request to that Action and sending that String or the Model.Email as a parameter.

3
  • 1
    are you sure it's not throwing an exception? Try adding a scheme ("http://") to the URI string Commented Sep 13, 2017 at 16:29
  • That seemed to help! But it still won't Post. Commented Sep 13, 2017 at 16:50
  • but what is the issue? Do you have exception, some message, etc.?? Commented Sep 13, 2017 at 16:59

1 Answer 1

2

You need to use a proper Uri and also await the Task being returned from the called method.

private async void BtnSubmitClicked(object sender, EventArgs eventArgs) {
    HttpResponseMessage response = await ResetPasswordAsync();
    App.Log(string.Format("Status Code: {0}", response.StatusCode));
}

public Task<HttpResponseMessage> ResetPasswordAsync() {
    var model = new ForgotPassword() {
        Email = Email.Text
    };
    var client = new HttpClient();
    client.BaseAddress = new Uri("http://192.168.79.119:10000");
    var json = JsonConvert.SerializeObject(model);
    var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
    var path = "api/api/Account/PasswordReset";
    return client.PostAsync(path, content); //the Address is correct
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.