Query RestEndPoint and Get values

432
5
Jump to solution
4 weeks ago
Highlighted
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

{
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
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
Highlighted
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.

Reply
0 Kudos
Highlighted
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.

Reply
0 Kudos
Highlighted
MVP Regular Contributor

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.

Highlighted
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?

Reply
0 Kudos
Highlighted
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