Restsharp Post Request - Body With X-Www-Form-Urlencoded Values

RestSharp post request - Body with x-www-form-urlencoded values

this working for me, it was generator from postman

        var token = new TokenValidation()
app_id = CloudConfigurationManager.GetSetting("appId"),
secret = CloudConfigurationManager.GetSetting("secret"),
grant_type = CloudConfigurationManager.GetSetting("grant_type"),
Username = CloudConfigurationManager.GetSetting("Username"),
Password = CloudConfigurationManager.GetSetting("Password"),

var client = new RestClient($"{xxx}{tokenEndPoint}");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", $"app_id={token.app_id}&secret={token.secret}&grant_type={token.grant_type}&Username={token.Username}&Password={token.Password}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

if (response.StatusCode != HttpStatusCode.OK)
Console.WriteLine("Access Token cannot obtain, process terminate");
return null;

var tokenResponse = JsonConvert.DeserializeObject<TokenValidationResponse>(response.Content);

RestSharp defaulting Content-Type to application/x-www-form-urlencoded on POST

It appears this is a misunderstanding of how RestSharp interprets parameters for post requests. From John Sheehan's post on the google group:

If it's a GET request, you can't have a request body and AddParameter
adds values to the URL querystring. If it's a POST you can't include a
POST parameter and a serialized request body since they occupy the
same space. You could do a multipart POST body but this is not very
common. Unfortunately if you're making a POST the only way to set the
URL querystring value is through either string concatenation or

var key = "12345";
var request = new RestRequest("api?key=" + key);
// or
var request = new RestRequest("api?key={key});
request.AddUrlSegment("key", "12345");

My revised Execute request method that now works looks like this:

private IRestResponse ExecuteRequestAsPost<T>(T model, string resource, Method method)
resource += "?auth_token={token}";
var client = CreateRestClient();
var request = new RestRequest(resource, method) { RequestFormat = DataFormat.Json };
var json = JsonConvert.SerializeObject(model);
request.AddHeader("User-Agent", "Fiddler");

request.AddUrlSegment("token", _apiKey);
request.AddParameter("application/json", json, ParameterType.RequestBody);

return client.Execute(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, "") { Content = new FormUrlEncodedContent(dict) };
var res = client.SendAsync(req).Result;

Restsharp header always set to "application/x-www-form-urlencoded" on POST request

If the request method is POST, AddParameter will add your parameter in the request body (if invoked using only two arguments).

If you need to put your parameter inside the query string you need to specify that explicitly:

request.AddParameter("apiKey", Common.API_KEY, ParameterType.QueryString);
// other segments omitted

Related Topics

Leave a reply