0

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.'

Stefan
  • 17,448
  • 11
  • 60
  • 79
YaRmgl
  • 356
  • 1
  • 6
  • 19
  • Not entirely sure, but I don't think you can pull two parameters from the body like that. The model binding will try to bind to a single parameter unless it's from different sources like the URL, or headers. Have you tried making a single class with login and password as properties then used that class as the parameter type? – Xipooo Sep 04 '19 at 19:18
  • That error message does not match the code in question. That usually is the result of a GET operation that tries to include a request message body which is not supported. Are you sure you rebuilt your code and that this Exception is being emitted on the code shown? – Igor Sep 04 '19 at 19:18
  • Does it help if you also set the request's content type to `application/x-www-form-urlencoded`? – Wiktor Zychla Sep 04 '19 at 19:22
  • @Igor it works fine with postman, only C# cannot get it. also this is asp.net 2.2 – YaRmgl Sep 04 '19 at 19:22
  • @WiktorZychla how do I set that? – YaRmgl Sep 04 '19 at 19:24
  • @YaRmgl: should I google that for you? – Wiktor Zychla Sep 04 '19 at 19:25
  • You did not answer my question. Make sure your code is rebuilt before you run it (do not accidentally run an older version due to a compile error). Also make sure the exception is being thrown by the code in question and not something that might be taking place before hand. I say this because the exception message does not match the code shown. – Igor Sep 04 '19 at 19:26
  • @YaRmgl: [here it is](https://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request), hope that helps. – Wiktor Zychla Sep 04 '19 at 19:26
  • httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); does not work, @Igor I will do a try/catch to see the errors – YaRmgl Sep 04 '19 at 19:30

1 Answers1

0

My bad, I am really blind, I was calling a deprecated GET from another route on the OnClick, although @WiktorZychla pointed out that I needed to set the content type to "application/x-www-form-urlencoded"

YaRmgl
  • 356
  • 1
  • 6
  • 19