addFeature to Feature Server using python

3543
1
06-22-2015 01:45 AM
ElliottCarson
New Contributor III

Hi,

I'm trying to add a feature to a Feature Server using python without any success.

When running the script I get the following error HTTPError: HTTP Error 401: Unauthorized.

The service only requires windows authentication of a user logged into the network, the service does not require a username or password.

How can this be achieved in Python?

My code so far is:

add = [{ "geometry" : {"x" : 20.8,"y" : -115.5},"attributes" : {"COMMENTS" : "Test"}}] 

service_url = r"http://<servername>/ags/rest/services/test/testlyr/FeatureServer/"

 

params = urllib.urlencode({'f': 'json', 'features': add}) 

req = urllib2.Request("{0}/addFeatures/?".format(service_url, params)) 

 

response = urllib2.urlopen(req) 

jsonResult = json.load(response) 

print jsonResult

Regards,

Elliott

0 Kudos
1 Reply
ElliottCarson
New Contributor III

I manage to get the following to work with a username an password.

If someone knows how to get the current users credentials and pass that instead it would be most helpful.

import urllib, urllib2

from ntlm import HTTPNtlmAuthHandler

user = 'DOMAIN\user'

password = "password"

url = "http://<server>/arcgis/rest/services/Test/FeatureServer/0/addFeatures"

updateAttr = [{ "geometry" : {"x" : XXX,"y" : XXX},"attributes" : {"COMMENTS" : "WORK"}}]

params = urllib.urlencode({'f': 'json', 'features': updateAttr})

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()

passman.add_password(None, url, user, password)

# create the NTLM authentication handler

auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)

# create and install the opener

opener = urllib2.build_opener(auth_NTLM)

urllib2.install_opener(opener)

url1 = urllib2.Request(updateBaseURL,params)

# retrieve the result

response = urllib2.urlopen(url1)

print(response.read())

0 Kudos