1

I am a SQL Developer, JSON and C# are completely new to me. I am trying to get the JSON data from Rest API from elastic DB using POST method. I think my code is incorrect since I am getting all the response from the API even though I am passing string json variable in my c# code.

       public static string COSMOS(string getAPiurl, string ApiUserId, string ApiPassword)
        {
            string url = getAPiurl;
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);

            webrequest.Method = "POST";
            webrequest.ContentType = "application/JSON";
            webrequest.Accept = "application/JSON";

            String username = ApiUserId;
            String password = ApiPassword;
            String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));

            webrequest.Headers.Add("Authorization", "Basic " + encoded);

            using (var streamWriter = new StreamWriter(webrequest.GetRequestStream()))
            {
                string json = "{\"query\":{\"bool\":{\"should\":[{\"match\":{\"platform\":{\"query\":\"COSMOS\"}}}],\"filter\":" +
                    "[{\"range\":{\"cu_ticketLastUpdated_date\":{\"gte\":\"2022-05-01 00:00:00\",\"lt\":" +
                    "\"2022-05-31 23:59:59\"}}}]}}}";

                streamWriter.Write(json);
            }

            var httpResponse = (HttpWebResponse)webrequest.GetResponse();


            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var rl = streamReader.ReadToEnd();
                httpResponse.Close();
            }

I am using the above code in my SSIS script component. When I am trying to debug the above code in the var rl I am getting all the response even though I am passing the filters like "filter":[{"range":{"cu_ticketLastUpdated_date":{"gte":"2022-05-01 00:00:00","lt":"2022-05-31 23:59:59"}}

Please help me with an solution.

Learner
  • 25
  • 5
  • Welcome to SO! Have you run your filters through a lint checker of some kind? I haven't done much with elastic, but I've run across something similar before and it just came down to the API not being able to understand the filter and is failing silently. – Kingsolmn Jun 03 '22 at 02:04

0 Answers0