How to Post Using Httpclient Content Type = Application/X-Www-Form-Urlencoded

HttpClient post async form-url-encoded and string content not working

Using FormUrlEncodedContent instead of string content fixed my problem :

var requestContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("username", username),
// ...
});
var response = await _httpClient.PostAsync(requestUri, requestContent);
var responseContent = await response.Content.ReadAsStringAsync();

How to send x-www-form-urlencoded in a post request in webclient?

You can use UploadString() method on WebClient class like

string data = "name=john&age=20&city=Uganda";
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string result = client.UploadString(url of api resource, data);
}

Passing data in application/x-www-form-urlencoded in HttpClient

  1. Use https://curl.olsh.me/ for curl commands to C# code
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api-url/id"))
{
var contentList = new List<string>();
contentList.Add($"data={Uri.EscapeDataString("P1;P2")}");
contentList.Add($"format={Uri.EscapeDataString("json")}");
request.Content = new StringContent(string.Join("&", contentList));
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");

var response = await httpClient.SendAsync(request);
}
}

Trying to make an API POST using application/x-www-form-urlencoded in a wpf desktop app

You can check something similar to this.

        HttpClient client = new HttpClient();

var dict = new Dictionary<string, string>();
dict.Add("sid", "mySID");
dict.Add("key", "myKey");
dict.Add("updatedDTM", "09/20/2021");

var req = new HttpRequestMessage(HttpMethod.Post, "https://app.agencybloc.com/api/v1/individuals/search/") { Content = new FormUrlEncodedContent(dict) };
var res = client.SendAsync(req).Result;

POST using HttpClient fro content type application/x-www-form-urlencoded using grant_type and scope

we'll i figured it out that i was missing the authorization setting in request :

req.Headers.Add("Authorization", "Basic " + auth); // auth will be string visible in postman after Basic

Placing this started giving me the result.

HttpClient using FormUrlEncodedContent

looks this!
the method GetResponse. I send an HttpRequestMessage (Request property) filled with the information that you are putting in the HttpClient.

public void SetContent<TEntity>(TEntity input)
{
var json = JsonConvert.SerializeObject(input);

if (Request.Method == HttpMethod.Post || Request.Method == HttpMethod.Put)
{
if (string.IsNullOrEmpty(ContentType) || ContentType == "application/json")
{
Request.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
else
{
var parameters = JsonConvert.DeserializeObject<Dictionary<string, string>>(json)
.Concat(CustomParameters)
.Where(p => !string.IsNullOrEmpty(p.Value));

Request.Content = new FormUrlEncodedContent(parameters);
}
}
}

public Out GetResponse<TEntity, Out>(HttpClient client, TEntity input)
{
var response = client.SendAsync(Request).Result;
var content = response.Content.ReadAsStringAsync().Result;

SetHttpClientDataToEntity(input, response);

if (!response.IsSuccessStatusCode)
{
ErrorDetail error;

throw content.TryParseJson(out error)
? new Exception(error.Message)
: throw new Exception(content);
}

Out result;
content.TryParseJson<Out>(out result);
return result;
}

angular http post with 'Content-Type': 'application/x-www-form-urlencoded' and responseType: 'text'

I ended up solve this by using fetch API:

`

import { from } from 'rxjs';

fetchTgtUrl(username, password) {
let loginUrl = '' + this._constants.getLoginUrl();
let bodyString = 'username=' + username.trim() + '&password=' + encodeURIComponent(password);
return fetch(loginUrl, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: bodyString
}).then(res => res.text());
}
getTGT(username, password): Observable<string> {
let tgt = this.fetchTgtUrl(username, password);
return from(tgt);
}

`



Related Topics



Leave a reply



Submit