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

  • Passing data from POSTMAN as x-www-form-urlencoded
  • Key and values are as follows:
data : P1;P2
format : json

Corresponding curl code from POSTMAN

curl --location --request POST 'https://ap-url/id/' \
--header 'content-type: application/x-www-form-urlencoded' \
--data-urlencode 'data=P1;P2' \

How to send the data as x-www-form-urlencoded on HttpClient?



Solution 1:[1]

The best Pattern is to set a dictionary and send this dictionary data in the post method

var client = _clientFactory.CreateClient("mobile-server");

                var data = new[]
                {
                    new KeyValuePair<string, string>("grant_type", "client_credentials"),
                    new KeyValuePair<string, string>("client_id",
                        _configuration.GetValue<string>("MobileTop:ClientId")),
                    new KeyValuePair<string, string>("client_secret",
                        _configuration.GetValue<string>("MobileTop:ClientSecret")),
                };
                var response =
                    await client.PostAsync("api/v2/connect/token", new FormUrlEncodedContent(data));    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sanjib Dhar