ArcGIS Rest API Add Attachment Issue

3095
3
Jump to solution
11-09-2016 06:02 PM
CallumSmith2
Occasional Contributor

Hi

I am writing a python script that copies data from 1 hosted feature layer to another feature layer. This is to move data captured with survey 123 to another instance when changes to the survey have been made like extra questions.

It is all working fine except for the attachments. I can get the attachment as a string using the Attachment request detailed here: ArcGIS REST API . What I want to do with this data is write it to another feature layer that has attachments enabled using the "Add Attachment" request detailed here: ArcGIS REST API .

I am passing the string that has come from the "Attachment" request as a attachment parameter but no mater what I do I get the error:

java.lang.String cannot be cast to com.esri.arcgis.discovery.json.JSONObject

Does anybody know what I need to do to the String object to be able to pass it to the "Add Attachment" request so that it works without error? Unfortunatley the documentation is a bit light!

Any help appreciated.

cheers

Callum

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
CallumSmith2
Occasional Contributor

After lost of trial and error I managed to get it to work using the requests module as is outlined in this post: Correct syntax for ???RESTful??? addAttachment? and this post Unable to add attachment to ArcGIS Online feature service using Python and REST API - Geographic Inf... 

cheers

Callum

View solution in original post

0 Kudos
3 Replies
CallumSmith2
Occasional Contributor

After lost of trial and error I managed to get it to work using the requests module as is outlined in this post: Correct syntax for ???RESTful??? addAttachment? and this post Unable to add attachment to ArcGIS Online feature service using Python and REST API - Geographic Inf... 

cheers

Callum

0 Kudos
mikAMD
by
Occasional Contributor II

For anybody that stumbles upon this thread looking for help, the trick was to decode the string. Here is what I did that finally worked (also to move/copy attachments from one feature to another):

 

import requests, base64, json

# get image string
urlAttachment = "https://services1.arcgis.com/d2krQaXxxwrynqoe/arcgis/rest/services/{}/FeatureServer/{}/{}/attachments/{}?f=json&token={}".format(feature_service_to_copy_from, feature_layer_id, feature_id, attachment_id, token)
response = requests.get(urlAttachment)
attachString = json.loads(response.text)["Attachment"]
# this is what took me a long time to figure out, decode the string
attachBinary = base64.b64decode(attachString)

# upload image to other feature service
params = {"f": "json", "token", token}
files = {"attachments": ('name_of_image.jpg', attachBinary, 'image/jpeg')}
urlAddAttachment = "https://services1.arcgis.com/d2krQaXxxwrynqoe/arcgis/rest/services/{}/FeatureServer/{}/{}/addAttachment".format(feature_service_to_copy_to, feature_layer_id, feature_id, token)
response = requests.post(urlAddAttachment, params=params, files=files)
result = json.loads(response.text)
if "addAttachmentResult" in result:
        if "success" in result["addAttachmentResult"]:
            if result["addAttachmentResult"]["success"] == True:
                print("SUCCESS!")

 


Also, did not have to change the headers or anything. It was always resulting in an error.

JessicaHanscomBrunswickGIS
New Contributor II

I just spent a week down this rabbit hole and your code here was the key. Thank you so much for posting this!

I think the params assignment should read like this though: params = {"f": "json", "token": token}

Also, I can't believe this was such a struggle and required so much digging to figure out. It seems like a super obvious thing people would want to do.

Again, thank you so much!