Prince Achim
Prince Achim

Reputation: 2161

How to POST using HTTPclient content type = application/x-www-form-urlencoded

I am currently developing a wp8.1 application C#, i have managed to perform a POST method in json to my api by creating a json object (bm) from textbox.texts. here is my code below. How do i take the same textbox.text and POST them as a content type = application/x-www-form-urlencoded. whats the code for that?

Profile bm = new Profile();
bm.first_name = Names.Text;
bm.surname = surname.Text;

string json = JsonConvert.SerializeObject(bm);

MessageDialog messageDialog = new MessageDialog(json);//Text should not be empty 
await messageDialog.ShowAsync();

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");

byte[] messageBytes = Encoding.UTF8.GetBytes(json);
var content = new ByteArrayContent(messageBytes);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = client.PostAsync("myapiurl", content).Result;

Upvotes: 215

Views: 277547

Answers (4)

Alexandru Dicu
Alexandru Dicu

Reputation: 1235

Another variant to POST this content type and which does not use a dictionary would be:

StringContent postData = new StringContent(JSON_CONTENT, Encoding.UTF8, "application/x-www-form-urlencoded");
using (HttpResponseMessage result = await httpClient.PostAsync(url, postData))
{
    if (response.IsSuccessStatusCode)
    {
        return response.Content.ReadAsStringAsync().Result;
    }
}

Upvotes: 1

Bhushan Teli
Bhushan Teli

Reputation: 911

 var parameters = new Dictionary<string, string>();
 var url = "Please enter URL here"; 
 parameters.Add("key1", "value1");
 parameters.Add("key2", "value2");
             
 using (HttpClient client = new HttpClient())
 {
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    HttpResponseMessage response = client.PostAsync(url, new FormUrlEncodedContent(parameters)).Result;
    var token = response.Content.ReadAsStringAsync().Result;
}
               
//Get response as expected

Upvotes: 77

Ray Krungkaew
Ray Krungkaew

Reputation: 6973

var nvc = new List<KeyValuePair<string, string>>();
nvc.Add(new KeyValuePair<string, string>("Input1", "TEST2"));
nvc.Add(new KeyValuePair<string, string>("Input2", "TEST2"));
using var client = new HttpClient();
using var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(nvc) };
using var res = await client.SendAsync(req);

Or

var dict = new Dictionary<string, string>();
dict.Add("Input1", "TEST2");
dict.Add("Input2", "TEST2");
using var client = new HttpClient();
using var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(dict) };
using var res = await client.SendAsync(req);

Upvotes: 370

Luiz Freitas
Luiz Freitas

Reputation: 389

The best solution for me is:

// Add key/value
var dict = new Dictionary<string, string>();
dict.Add("Content-Type", "application/x-www-form-urlencoded");

// Execute post method
using (var response = httpClient.PostAsync(path, new FormUrlEncodedContent(dict))){}

Upvotes: 28

Related Questions