I'm trying to POST form data to an ASP.NET Core API, the controller is set up this way:
[HttpPost]
public ActionResult<string> Post(
[FromForm]string loginname,
[FromForm]string password)
{
// do stuff
return JsonConvert.SerializeObject(response);
}
and this is the HttpClient in the Xamarin.Forms app:
private async Task<LoginResponse> Login(string loginname, string password, string ip)
{
string url = "http://192.168.1.27:5000/api/login";
using (var httpClient = new HttpClient())
{
HttpContent formdata = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("loginname", loginname),
new KeyValuePair<string, string>("password", password)
});
HttpResponseMessage response;
using (var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = formdata })
response = await httpClient.SendAsync(req);
return JsonConvert.DeserializeObject<LoginResponse>(response.ToString());
}
}
I keep getting the following error after trying for so much time:
HttpClient error: System.Net.WebException: 'Cannot send a content-body with this verb-type.'