2

使用 C#、.Net 4,5、RestSharp v4.0.3

尝试在 GoCardless 中创建 api_key

我像这样创建一个 RestClient :

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");

当我发布到 GoCardless 时出现错误

{"error":{"message":"'Content-Type' header must be application/json or application/vnd.api+json ......
4

1 回答 1

0

经过大量徒劳的搜索后,我放弃并使用了旧 StackOverflow 帖子中的解决方案。如

// 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);

我可以看到为什么会这样 - 甚至可能是 RestSharp 这样做的意图。

于 2015-02-27T12:53:59.807 回答