<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Looking for a code example of using Python REST API for applyEdits in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1641037#M74593</link>
    <description>&lt;P&gt;I believe that the httpx library has been added to the latest version of arcpy's environment:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import httpx
import asyncio


def get_requests(url: str, edits: list[httpx.QueryParams], client: httpx.AsyncClient):
    """Build requests using the edit params and the provided client"""
    yield from [client.build_request("POST", url, params=update) for update in edits]


async def apply_edits(url: str, edits: list[httpx.QueryParams]):
    async with httpx.AsyncClient() as client:
        # Add tasks to running async loop
        tasks = [client.send(request) for request in get_requests(url, edits, client)]
        print(f"Applying {len(tasks)} edits...")

        # Await the responses asyncronously (send them all at once and wait for them to respond)
        responses = await asyncio.gather(*tasks, return_exceptions=True)

        # Filter the responses and check for failed transactions
        valid_responses: list[httpx.Response] = []
        for response in responses:
            if not isinstance(response, httpx.Response):
                print(f"[ERROR]: {response}")
            elif response.status_code != 200:
                print(f"[ERROR]: &amp;lt;{response.status_code}&amp;gt; {response.reason_phrase}")
            else:
                valid_responses.append(response)

        print(f"Successfully applied {len(valid_responses)} edits!")

    # Return the json values for all responses
    return [r.json() for r in valid_responses]


async def main():
    URL = "&amp;lt;my_url&amp;gt;/FeatureServer/0/applyEdits"
    
    # Convert the dict to a QueryParams object (optional), QueryParams is just
    # a MultiDict so x['a'] = 1 follwed by x['a'] = 2 creates a dict with x['a'] = [1, 2]
    updates: list[dict] = [{"attributes": {"objectid": 2, "is_copied": 0}}]
    edits: list[dict] = [{'updates': updates, 'f' : 'json'}]
    queries = list(map(httpx.QueryParams, edits))
    
    # Dispatch and await the responses
    responses = await apply_edits(URL, queries)
    
    # Do something with the repsonses
    print(responses)


if __name__ == "__main__":
    asyncio.run(main())&lt;/LI-CODE&gt;&lt;P&gt;If you need to deal with tons of queries like this, it's definitely worth looking into as it allows you to add the requests to an event loop ([send, send, send -&amp;gt; wait, wait wait] instead of [send -&amp;gt; wait, send -&amp;gt; wait, send -&amp;gt; wait]) so you can dispatch as many as the server can handle at once.&lt;/P&gt;</description>
    <pubDate>Tue, 12 Aug 2025 18:17:48 GMT</pubDate>
    <dc:creator>HaydenWelch</dc:creator>
    <dc:date>2025-08-12T18:17:48Z</dc:date>
    <item>
      <title>Looking for a code example of using Python REST API for applyEdits</title>
      <link>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1639985#M74585</link>
      <description>&lt;P&gt;Does anybody have an Python code example of submitting an applyEdits to the ArcGIS REST API? I'm spending way too much time trying to get a very simple update to work.&amp;nbsp; &amp;nbsp;I tried to use the API for Python but ran into a &lt;A href="https://community.esri.com/t5/arcgis-api-for-python-questions/layer-edit-features-hangs/m-p/1639907#M11553" target="_self"&gt;problem&lt;/A&gt; with that so I thought the REST API might work better. I think my problem is how I'm encoding the parameters but not sure.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did a browser trace doing the update manually with the ArcGIS REST Services Directory tool and this is the form data that gets sent - I'm just not sure how to make that happen with Python.&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;adds=&amp;amp;updates=%5B%7B%22attributes%22%3A+%7B%22objectid%22%3A+zz%2C+%22is_copied%22%3A+1%7D%7D%5D&amp;amp;deletes=&amp;amp;gdbVersion=&amp;amp;rollbackOnFailure=true&amp;amp;useGlobalIds=false&amp;amp;returnEditMoment=false&amp;amp;trueCurveClient=true&amp;amp;attachments=&amp;amp;timeReferenceUnknownClient=false&amp;amp;datumTransformation=&amp;amp;editsUploadId=&amp;amp;async=false&amp;amp;returnEditResults=true&amp;amp;f=html&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Aug 2025 13:41:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1639985#M74585</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2025-08-07T13:41:41Z</dc:date>
    </item>
    <item>
      <title>Re: Looking for a code example of using Python REST API for applyEdits</title>
      <link>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1640137#M74586</link>
      <description>&lt;P&gt;I finally figured it out. Here is some code that does work:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import urllib
import urllib.request
import json
from urllib.parse import urlencode 

url = '&amp;lt;my_url&amp;gt;/FeatureServer/0/applyEdits'
updates = [
      {
            "attributes": {"objectid": 2, "is_copied": 0} 
      }
]
params = dict()
params['updates'] = updates
params['f'] = 'json'
req = urllib.request.Request(url, urlencode(params))
req.data = req.data.encode('utf-8')
response = urllib.request.urlopen(req) 
data = json.load(response)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 07 Aug 2025 18:47:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1640137#M74586</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2025-08-07T18:47:22Z</dc:date>
    </item>
    <item>
      <title>Re: Looking for a code example of using Python REST API for applyEdits</title>
      <link>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1640154#M74587</link>
      <description>&lt;P&gt;This looks pretty good! The only thing that you might need to do is generate a token for access in the event that the service requires authentication. That request would look like this, then you would just need to add 'token' to the params.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;url = "https://arcgis.com/sharing/rest/generateToken"
d= {
    'username' : un,
    'password' : pw,
    'client' : 'referer',
    'referer' : 'https://.arcgis.com',
    'f' : 'json'
}
token_resposne = urllib.request.Request(url, urlencode(d))
token = token_response.json()['token']&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 07 Aug 2025 19:44:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1640154#M74587</guid>
      <dc:creator>AustinAverill</dc:creator>
      <dc:date>2025-08-07T19:44:11Z</dc:date>
    </item>
    <item>
      <title>Re: Looking for a code example of using Python REST API for applyEdits</title>
      <link>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1641037#M74593</link>
      <description>&lt;P&gt;I believe that the httpx library has been added to the latest version of arcpy's environment:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import httpx
import asyncio


def get_requests(url: str, edits: list[httpx.QueryParams], client: httpx.AsyncClient):
    """Build requests using the edit params and the provided client"""
    yield from [client.build_request("POST", url, params=update) for update in edits]


async def apply_edits(url: str, edits: list[httpx.QueryParams]):
    async with httpx.AsyncClient() as client:
        # Add tasks to running async loop
        tasks = [client.send(request) for request in get_requests(url, edits, client)]
        print(f"Applying {len(tasks)} edits...")

        # Await the responses asyncronously (send them all at once and wait for them to respond)
        responses = await asyncio.gather(*tasks, return_exceptions=True)

        # Filter the responses and check for failed transactions
        valid_responses: list[httpx.Response] = []
        for response in responses:
            if not isinstance(response, httpx.Response):
                print(f"[ERROR]: {response}")
            elif response.status_code != 200:
                print(f"[ERROR]: &amp;lt;{response.status_code}&amp;gt; {response.reason_phrase}")
            else:
                valid_responses.append(response)

        print(f"Successfully applied {len(valid_responses)} edits!")

    # Return the json values for all responses
    return [r.json() for r in valid_responses]


async def main():
    URL = "&amp;lt;my_url&amp;gt;/FeatureServer/0/applyEdits"
    
    # Convert the dict to a QueryParams object (optional), QueryParams is just
    # a MultiDict so x['a'] = 1 follwed by x['a'] = 2 creates a dict with x['a'] = [1, 2]
    updates: list[dict] = [{"attributes": {"objectid": 2, "is_copied": 0}}]
    edits: list[dict] = [{'updates': updates, 'f' : 'json'}]
    queries = list(map(httpx.QueryParams, edits))
    
    # Dispatch and await the responses
    responses = await apply_edits(URL, queries)
    
    # Do something with the repsonses
    print(responses)


if __name__ == "__main__":
    asyncio.run(main())&lt;/LI-CODE&gt;&lt;P&gt;If you need to deal with tons of queries like this, it's definitely worth looking into as it allows you to add the requests to an event loop ([send, send, send -&amp;gt; wait, wait wait] instead of [send -&amp;gt; wait, send -&amp;gt; wait, send -&amp;gt; wait]) so you can dispatch as many as the server can handle at once.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Aug 2025 18:17:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1641037#M74593</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-08-12T18:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: Looking for a code example of using Python REST API for applyEdits</title>
      <link>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1641044#M74594</link>
      <description>&lt;P&gt;I was gonna say you usually want to encode that information in the headers, but apparently ArcGIS expects it to be sent as &lt;A href="https://developers.arcgis.com/rest/users-groups-and-items/generate-token/" target="_self"&gt;request params&lt;/A&gt;, weird... Seems like a great way to open up a &lt;A href="https://owasp.org/www-community/attacks/Web_Parameter_Tampering" target="_self"&gt;Parameter Tampering vector&lt;/A&gt; . They don't seem to use headers for much.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Aug 2025 19:24:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1641044#M74594</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-08-11T19:24:52Z</dc:date>
    </item>
    <item>
      <title>Re: Looking for a code example of using Python REST API for applyEdits</title>
      <link>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1641133#M74595</link>
      <description>&lt;P&gt;My current use case is very low volume but your example looks like a better alternative to some other work I've done using the multiprocessing package to spin off parallel requests.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Aug 2025 04:19:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1641133#M74595</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2025-08-12T04:19:14Z</dc:date>
    </item>
    <item>
      <title>Re: Looking for a code example of using Python REST API for applyEdits</title>
      <link>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1641208#M74596</link>
      <description>&lt;P&gt;Yeah, one would think. But here we are lol.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Aug 2025 12:58:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/looking-for-a-code-example-of-using-python-rest/m-p/1641208#M74596</guid>
      <dc:creator>AustinAverill</dc:creator>
      <dc:date>2025-08-12T12:58:31Z</dc:date>
    </item>
  </channel>
</rss>

