Query RestEndPoint and Get values

2434
5
Jump to solution
10-30-2020 05:31 PM
jaykapalczynski
Frequent Contributor

I am simply trying to query a rest endpoint (Feature Service) and get values from Specific fields

I dont think I need all the SDKs etc....I just want to read the result of a JSON string after querying a Rest EndPoint with a WHERE Clause.

Thoughts? Examples?

private void button1_Click(object sender, EventArgs e)

    // Query a specific Rest EndPoint WHERE ID = ????

    // Write Field 1, Field 2, Field 6 to variables

    // Set variables to Textbox on Form

{
0 Kudos
1 Solution

Accepted Solutions
jaykapalczynski
Frequent Contributor

I think I got it working with this...Still testing

I added a section in there to specific outfields....this seems to be working as well....going to close this

            try
            {
            string gpUrl = "https://xxx.xxx.xxxxxxxx.xxx/arcgis/rest/services/N/F/MapServer/0";
            string whereClause = "OBJECTID>1";
            string[] outfieldsParam = new string[2];
                outfieldsParam[0] = "Address";
                outfieldsParam[1] = "State";
            string outfieldsParam2 = string.Join(",", outfieldsParam); 
            //string requestUrl = gpUrl + "/query?where=" + whereClause ;
            string requestUrl = gpUrl + "/query?where=" + whereClause + "&outfields=" + outfieldsParam2;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(requestUrl));

            request.KeepAlive = true;
            request.Method = "POST";
            request.AllowAutoRedirect = true;
            request.CookieContainer = new System.Net.CookieContainer();
            request.ContentType = "application/x-www-form-urlencoded";

            StringBuilder paramString = new StringBuilder();
            paramString.Append($"&f=json");

            byte[] data = Encoding.ASCII.GetBytes(paramString.ToString());
            request.ContentLength = data.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            string responseStr = getStringFromWebResponse(response);
            Response = responseStr;
            return (!(responseStr.Contains("error")));
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }

View solution in original post

5 Replies
jaykapalczynski
Frequent Contributor

If I can simply get the entire JSON string back I would be fine...I can parse that out myself...

Not sure how to query the Service with the WHERE clause....examples I have seen are lik 50 lines of code....I dont think it should be that complicated or I am missing something.

0 Kudos
jaykapalczynski
Frequent Contributor

I can run this from the REP and get a return

How do I turn this into a C# call from an ASPX .net page Button click?  This returns me the JSON I can filter through and get the values I need.

0 Kudos
RandyBurton
MVP Alum

You button would need to trigger an HTTP post request.  Code for python would be something like:

# URL address of feature, similar to:
URL = "https://services.arcgis.com/<abcdef>/arcgis/rest/services/<layer>/FeatureServer/0/query"
dataFlds = ["FldOne", "FldTwo"]
query_dict = {
    "where" : "EditDate >= DATE '{}'".format(editDate),
    "outFields" : ",".join(dataFlds),
    "orderByFields" : "EditDate",
    "returnGeometry" : "true",
    "outSR" : "4326", # lon, lat
    "f": "json", "token": token['token'] }

r = requests.post(url = URL, data = query_dict)
returnJson = json.loads(r.content)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Look at the HTML source of the page in your picture and you can see the names of the various form elements.  In addition to the where query, looks like you will also need to set "f" to the return format you desire and probably include a token.

jaykapalczynski
Frequent Contributor

But I am doing this in C# .aspx app....not a python app....how do  I make the call from the C# aspx app?

0 Kudos
jaykapalczynski
Frequent Contributor

I think I got it working with this...Still testing

I added a section in there to specific outfields....this seems to be working as well....going to close this

            try
            {
            string gpUrl = "https://xxx.xxx.xxxxxxxx.xxx/arcgis/rest/services/N/F/MapServer/0";
            string whereClause = "OBJECTID>1";
            string[] outfieldsParam = new string[2];
                outfieldsParam[0] = "Address";
                outfieldsParam[1] = "State";
            string outfieldsParam2 = string.Join(",", outfieldsParam); 
            //string requestUrl = gpUrl + "/query?where=" + whereClause ;
            string requestUrl = gpUrl + "/query?where=" + whereClause + "&outfields=" + outfieldsParam2;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(requestUrl));

            request.KeepAlive = true;
            request.Method = "POST";
            request.AllowAutoRedirect = true;
            request.CookieContainer = new System.Net.CookieContainer();
            request.ContentType = "application/x-www-form-urlencoded";

            StringBuilder paramString = new StringBuilder();
            paramString.Append($"&f=json");

            byte[] data = Encoding.ASCII.GetBytes(paramString.ToString());
            request.ContentLength = data.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            string responseStr = getStringFromWebResponse(response);
            Response = responseStr;
            return (!(responseStr.Contains("error")));
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }