Skip to content

Instantly share code, notes, and snippets.

@mfcallahan
Created February 12, 2024 19:16
Show Gist options
  • Save mfcallahan/c8472256fd8559094fe85a4bee18ef0e to your computer and use it in GitHub Desktop.
Save mfcallahan/c8472256fd8559094fe85a4bee18ef0e to your computer and use it in GitHub Desktop.
ArcGIS Feature Service query using REST endpoint
import requests
def generateToken():
# substitiue your ArcGIS credentials here
userName = "username"
password = "password"
generateTokenUrl = "https://www.arcgis.com/sharing/rest/generateToken"
refererUrl = "https://www.arcgis.com&client=referer"
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
requestBody = f"username={userName}&password={password}&referer={refererUrl}&f=json"
response = requests.request("POST", generateTokenUrl, headers=headers, data=requestBody)
return response.json()["token"]
def queryTable(token):
serviceUrl = "https://services2.arcgis.com/FiaPA4ga0iQKduv3/ArcGIS/rest/services/Colleges_and_Universities_View/FeatureServer/0/query"
city = "Phoenix"
state = "AZ"
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
requestBody = f"where=CITY='{city}' AND STABBR='{state}'&outFields=*&returnGeometry=true&f=json&token={token}"
response = requests.request("POST", serviceUrl, headers=headers, data=requestBody)
return response.json()
token = generateToken()
response = queryTable(token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment