Revulytics event tracking returning Bad Request

时间:2017-10-08 07:48:51

标签: c# json bad-request

I am testing the Revulytics reporting API and I am trying to generate an advanced event tracking report, as per http://devzone.revulytics.com/docs/API/event-tracking-advanced-reports.html#. I use C# and the WebClient class to send the HTTP query (and Newtonsoft for JSON parsing and formatting). However, although my JSON is formatted properly and the client is properly configured, I get an unhandled exception. Code sample below:

    RuiAdvancedRequest advancedRequest = new RuiAdvancedRequest();
    DateTime startDate = DateTime.Now.AddDays(-180);
    advancedRequest.user = textUser.Text;
    advancedRequest.sessionId = authResponseObj.sessionId;
    advancedRequest.productId = Convert.ToInt64(textProductId.Text);
    advancedRequest.startDate = startDate.ToString("yyyy-MM-dd");
    advancedRequest.stopDate = DateTime.Now.Date.ToString("yyyy-MM-dd");
    advancedRequest.events = list;
    string serializedRequest = JsonConvert.SerializeObject(advancedRequest);
    string reportResult = client.UploadString(featureuUsageURL, "POST", serializedRequest);

The last line fails with: An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: The remote server returned an error: (400) Bad Request. Is there a way to get more error information from the Revulytics server?

1 个答案:

答案 0 :(得分:0)

尝试处理异常并查看错误响应流。我得到了一个类似的错误:虽然JSON格式正确,如果从服务器的角度来看params的值不正确,它仍然会返回此错误 - 这与JSON格式不正确的情况相同。

try
{
  reportResult = client.UploadString(featureuUsageURL, "POST",    serializedRequest);
}
catch (WebException error)
{
  string responseText;
  var responseStream = error.Response?.GetResponseStream();
  if (responseStream != null)
  {
    using (var reader = new StreamReader(responseStream))
  {
responseText = reader.ReadToEnd();
MessageBox.Show(responseText);
}    

希望这有帮助