David Parsonson
David Parsonson

Reputation: 575

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

Upvotes: 1

Views: 319

Answers (1)

Thousif Shaik
Thousif Shaik

Reputation: 59

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/

Upvotes: 1

Related Questions