'RestSharp body always empty

I need to call a Post service with RestSharp

 var client = new RestClient(url);
 var request = new RestRequest(url,Method.POST);
 request.AddHeader("Content-type", "application/json");
 request.AddJsonBody(new  { grant_type = "client_credentials", client_id = clientIdParam, client_secret = appReg_clientSecret, ressource = _ressource }
               ); 
 var response = client.Post(request);

The problem is that the request's body is always null and the json body is added as a parameter

enter image description here

Any ideas?



Solution 1:[1]

https://login.microsoftonline.com/{{tenantId}}/oauth2/token seems to not accept json, even with the content-type set as "application/json"

My solution:

var client = new RestClient("https://login.microsoftonline.com");
var request = new RestRequest("/{{tenantId}}/oauth2/token", Method.POST);

var requestBody = $"grant_type=client_credentials&client_id={clientIdParam}&client_secret={appReg_clientSecret}&resource={_resource}";

request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", requestBody, ParameterType.RequestBody);

var response = client.Execute(request);

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 Steepho