【问题标题】:invalidJsonBody error posting to log analytics rest apiinvalidJsonBody 错误发布到日志分析 rest api
【发布时间】:2017-09-14 06:30:50
【问题描述】:

您好,我正在尝试新的日志分析 REST api 示例,可以在此处找到:https://dev.loganalytics.io/documentation/Authorization/API-keys,但在我的标题完整代码中遇到上述错误:"{\"error\":{\"message\":\"The request had some invalid properties\",\"code\":\"BadArgumentError\",\"innererror\":{\"code\":\"QueryValidationError\",\"message\":\"Failed parsing the query\",\"details\":[{\"code\":\"InvalidJsonBody\",\"message\":\"Unexpected token \\\"\",\"target\":null}]}}}"

我的代码可以在下面找到我觉得我可能在这里遗漏了一些简单的东西,但我不知道我哪里出错了:

static void Main(string[] args)
    {
        try
        {
            var client = new Program();
            client.ExecAsync().Wait();
        }
        catch(Exception e)
        {
            Console.WriteLine(e);
            Console.ReadLine();
        }
    }

    async Task ExecAsync()
    {
        var content = new StringContent(GetQueryString(), Encoding.UTF8, "application/json");
        content.Headers.Add("X-Api-Key", "DEMO_KEY");
        var response = await client.PostAsync("https://api.loganalytics.io/v1/workspaces/DEMO_WORKSPACE/query", content);
        var responseString = await response.Content.ReadAsStringAsync();
        Console.WriteLine(responseString.Length);
        Console.ReadLine();
    }

private string GetQueryString()
    {
        return JsonConvert.SerializeObject("{\"query\":\"AzureActivity | summarize count() by Category}");
    }

【问题讨论】:

    标签: c# json rest http post


    【解决方案1】:

    我注意到您的 json 字符串中缺少双引号。当我做了一个小的 java sysout

    System.out.println("{\"query\":\"AzureActivity | summarize count() by Category}");
    

    它给了我这个输出。

    {"query":"AzureActivity | summarize count() by Category}
    

    - 类别后缺少双引号 }

    在你的代码中试试这个字符串:

    {\"query\":\"AzureActivity | summarize count() by Category\"}
    

    您可以尝试一些在线 json 验证器,例如 https://jsonlint.com/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 2021-11-29
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      相关资源
      最近更新 更多