简体   繁体   中英

invalidJsonBody error posting to log analytics rest api

Hi im trying out the new log analytics REST api example that can be found here: https://dev.loganalytics.io/documentation/Authorization/API-keys but am running into the aforementioned error in my title full code here: "{\\"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}]}}}"

my code for this can be found below i feel like im probably missing something simple here but i dont know where i'm going wrong:

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

I noticed that you are missing double quotes in your json string. When I did a small java sysout

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

it gave me this output.

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

Double quotes is missing after - Category" }

Try this string in your code:

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

You can try some online json validators like https://jsonlint.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM