ArcGIS REST API - upload zip file and update hosted feature service (not using arcgis)

374
1
01-22-2024 08:58 PM
RichardButgereit
Occasional Contributor

I need a Python script that uses ArcGIS REST API to upload a zip file and update a hosted feature service -- NOT using the arcgis package. I trying to follow Update

Here's what I have so far -- but nothing is successfully posting or updating. 

import requests
import json
import os
from datetime import datetime

now = datetime.now()
# dd/mm/YY H:M:S
dt_string = now.strftime("%m/%d/%Y")

# ArcGIS REST API URL for the item to be updated
item_id = "itemid"
url = f"https://myorg.maps.arcgis.com/sharing/rest/content/items/{item_id}/update"

# Get token
baseURL = "https://www.arcgis.com"
loginURL = "https://www.arcgis.com/sharing/rest/generateToken".format(baseURL)
username = "username"
password = "password"
expiration = 60

loginparams = {'username': username, 'password': password, 'client': 'referer', 'referer': baseURL, 'expiration': expiration, 'f': 'json'}
loginresponse = requests.post(loginURL, data=loginparams, verify=False)
token = loginresponse.json()['token']

# Path to the updated zipped shapefile
updated_shapefile_path = "C:\\GIS\\myzip.zip"

# Prepare the request headers
headers = {
"Content-Type": "multipart/form-data",
}

# Prepare the request parameters
params = {
"f": "json",
"title": f"My layer {dt_string}",
"description": f"My description - Last downloaded {dt_string}",
"snippet": f"My snippet - Last updated {dt_string}",
"token": token
}

# Open the shapefile in binary mode
with open(updated_shapefile_path, "rb") as file:
files = {"file": (os.path.basename(updated_shapefile_path), file)}
try:
response = requests.post(url, params=params, headers=headers, files=files)
response.raise_for_status()
if response.status_code == 200:
print("Shapefile updated successfully.")
else:
print(f"Failed to update shapefile. Status code: {response.status_code}")
print(response.text)
except requests.exceptions.RequestException as e:
print(f"Error during shapefile update: {e}")

# Check the response
if response.status_code == 200:
print("Shapefile updated successfully.")
else:
print(f"Failed to update shapefile. Status code: {response.status_code}")
print(response.text)

Tags (2)
0 Kudos
1 Reply
JohnEvans6
New Contributor III

I'm actually stuck on something similar enough. It does look like there are 2 operations to do this:

 
I cannot for the life of me figure out the appropriate parameters for the post request; the response is just the upload item html page. It does look like the order of operations is to upload the shapefile to the feature service, get the item ID, then take the item id and append/upsert. I can do it this the browser just not via script.
 
Via frontend
upload.PNG
 
Does not work.

 

# Upload to our url and get our upload ID
featureservice_url = featureservice_url + "/uploads/upload"

# set req headers
headers = {
    "Content-Type": "multipart/form-data"
}

# Get file and set data parameters
with open(shapefile_loc, 'rb') as f:

    # file upload params
    uploadparams = {
        'item': f,
        'f': json
    }

    r = requests.post(featureservice_url, data=uploadparams, headers=headers)

 

 
0 Kudos