2

Using C#, .Net 4,5, RestSharp v4.0.3

Attempting to create an api_key in GoCardless

I create a RestClient like this:

var client = new RestClient();

client.BaseUrl = SandboxBaseUrl;
client.Authenticator = new HttpBasicAuthenticator(apiKeyId, apiKey);
client.AddDefaultHeader("GoCardless-Version", Properties.Settings.Default.GoCardlessVersion);

client.AddDefaultHeader("Accept", "application/json");
client.AddDefaultHeader("content-type", "application/json");
request.AddHeader("content-type", "application/json");

When I post to GoCardless I get the error

{"error":{"message":"'Content-Type' header must be application/json or application/vnd.api+json ......
  • possible duplicate of [Does RestSharp overwrite manually set Content-Type?](http://stackoverflow.com/questions/9432436/does-restsharp-overwrite-manually-set-content-type) – Residuum Feb 25 '15 at 16:38
  • That was for a much earlier version of RestSharp in 2012 – Andrew Bingham Feb 25 '15 at 17:11

1 Answers1

0

After a lot of fruitless searching I gave up and used the solutions in older StackOverflow postings. Such as

// Create the Json for the new object
StringBuilder requestJson = new StringBuilder();
var requestObject = new { api_keys = new { name = name, links = new { role = role } } };
(new JavaScriptSerializer()).Serialize(requestObject, requestJson);
...
// Set up the RestSharp request
request.Parameters.Clear();
request.AddParameter("application/json", requestJson, ParameterType.RequestBody);

I can see why this works - and even perhaps the intention of RestSharp doing it this way.