Search code examples
c#xamarinxamarin.formsx-www-form-urlencoded

Xamarin.Forms Send Data To API as application/x-www-form-urlencoded


I want to send the data to an api as x-www-form-urlencoded not as json. I usually send the data as json using newtonsoft.json nuget package, I searched but did not find any solution. Thansks in advance


Solution

  • You can send string data and the content-type set to x-www-form-urlencoded

    using(var content=new StringContent())
    {
        content.Headers.Remove("Content-Type");
        content.Headers.Add("Content-Type","x-www-form-urlencoded");
    }
    

    If your parameters is username and password you can do this:

    string param="username=admin&password=123456";
    var content=new StringContent(param,Encoding.UTF8,"x-www-form-urlencoded");
    var client=new HttpClient();
    client.BaseAddress="https://localhost";
    await client.PostAsync("/api/login",content);