GoToWebinar - 请求不是预期的格式 - RestSharp

时间:2012-04-30 18:54:00

标签: c# asp.net json api restsharp

与此论坛帖子类似: https://developer.citrixonline.com/forum/request-not-expected-format

然而,他并没有真正解释他发现他的代码有什么问题。

我正在使用RestSharp进行API调用。我已经能够很好地工作以提取即将到来的网络研讨会列表,但是当我尝试注册参与者时,我一直得到400 /请求没有预期的格式错误。

遵循此文档https://developer.citrixonline.com/api/gotowebinar-rest-api/apimethod/create-registrant,这是我的代码:

var client = new RestClient(string.Format("https://api.citrixonline.com/G2W/rest/organizers/{0}/webinars/{1}/registrants", "300000000000xxxxxx", btn.CommandArgument));
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;

request.AddHeader("Accept", "application/json");
request.AddHeader("Accept", "application/vnd.citrix.g2wapi-v1.1+json");
request.AddHeader("Authorization", "OAuth oauth_token=" + System.Configuration.ConfigurationManager.AppSettings["GoToWebinar"]);

request.AddParameter("firstName", LoggedInUser.FirstName);
request.AddParameter("lastName", LoggedInUser.LastName);
request.AddParameter("email", LoggedInUser.Email);

var response = client.Execute(request);
var statusCode = response.StatusCode;

有关我如何弄清楚为什么会一直收到错误的任何见解?

谢谢!

1 个答案:

答案 0 :(得分:1)

不是使用AddParamter(将键/值参数添加到请求主体),而是需要编写JSON:

request.DataFormat = DataFormat.Json;
request.AddBody(new {
    firstName = LoggedInUser.FirstName,
    lastName = LoggedInUser.LastName,
    email = LoggedInUser.Email
});

如果要直接指定处理程序,还需要清除处理程序(自动设置Accept标头):

client.ClearHandlers();