<?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 ArcGIS Rest API Add Attachment Issue in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcgis-rest-api-add-attachment-issue/m-p/106061#M8170</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It is all working fine except for the attachments. I can get the attachment as a string using the Attachment request detailed here:&amp;nbsp;&lt;A class="link-titled" href="http://resources.arcgis.com/en/help/arcgis-rest-api/#/Attachment_Feature_Service/02r3000000r2000000/" title="http://resources.arcgis.com/en/help/arcgis-rest-api/#/Attachment_Feature_Service/02r3000000r2000000/"&gt;ArcGIS REST API&lt;/A&gt;&amp;nbsp;. 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:&amp;nbsp;&lt;A class="link-titled" href="http://resources.arcgis.com/en/help/arcgis-rest-api/#/Add_Attachment/02r3000000wt000000/" title="http://resources.arcgis.com/en/help/arcgis-rest-api/#/Add_Attachment/02r3000000wt000000/"&gt;ArcGIS REST API&lt;/A&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;java.lang.String cannot be cast to com.esri.arcgis.discovery.json.JSONObject&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;cheers&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Callum&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 10 Nov 2016 02:02:40 GMT</pubDate>
    <dc:creator>CallumSmith2</dc:creator>
    <dc:date>2016-11-10T02:02:40Z</dc:date>
    <item>
      <title>ArcGIS Rest API Add Attachment Issue</title>
      <link>https://community.esri.com/t5/python-questions/arcgis-rest-api-add-attachment-issue/m-p/106061#M8170</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It is all working fine except for the attachments. I can get the attachment as a string using the Attachment request detailed here:&amp;nbsp;&lt;A class="link-titled" href="http://resources.arcgis.com/en/help/arcgis-rest-api/#/Attachment_Feature_Service/02r3000000r2000000/" title="http://resources.arcgis.com/en/help/arcgis-rest-api/#/Attachment_Feature_Service/02r3000000r2000000/"&gt;ArcGIS REST API&lt;/A&gt;&amp;nbsp;. 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:&amp;nbsp;&lt;A class="link-titled" href="http://resources.arcgis.com/en/help/arcgis-rest-api/#/Add_Attachment/02r3000000wt000000/" title="http://resources.arcgis.com/en/help/arcgis-rest-api/#/Add_Attachment/02r3000000wt000000/"&gt;ArcGIS REST API&lt;/A&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;java.lang.String cannot be cast to com.esri.arcgis.discovery.json.JSONObject&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;cheers&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Callum&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Nov 2016 02:02:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcgis-rest-api-add-attachment-issue/m-p/106061#M8170</guid>
      <dc:creator>CallumSmith2</dc:creator>
      <dc:date>2016-11-10T02:02:40Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Rest API Add Attachment Issue</title>
      <link>https://community.esri.com/t5/python-questions/arcgis-rest-api-add-attachment-issue/m-p/106062#M8171</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;After lost of trial and error I managed to get it to work using the requests module as is outlined in this post:&amp;nbsp;&lt;A href="https://community.esri.com/thread/91378"&gt;Correct syntax for ???RESTful??? addAttachment?&lt;/A&gt;&amp;nbsp;and this post&amp;nbsp;&lt;A class="link-titled" href="http://gis.stackexchange.com/questions/146963/unable-to-add-attachment-to-arcgis-online-feature-service-using-python-and-rest" title="http://gis.stackexchange.com/questions/146963/unable-to-add-attachment-to-arcgis-online-feature-service-using-python-and-rest"&gt;Unable to add attachment to ArcGIS Online feature service using Python and REST API - Geographic Information Systems Sta…&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;cheers&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Callum&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Nov 2016 22:30:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcgis-rest-api-add-attachment-issue/m-p/106062#M8171</guid>
      <dc:creator>CallumSmith2</dc:creator>
      <dc:date>2016-11-10T22:30:02Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Rest API Add Attachment Issue</title>
      <link>https://community.esri.com/t5/python-questions/arcgis-rest-api-add-attachment-issue/m-p/1205534#M65394</link>
      <description>&lt;P&gt;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):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests, base64, json

# get image string
urlAttachment = "https://services1.arcgis.com/d2krQaXxxwrynqoe/arcgis/rest/services/{}/FeatureServer/{}/{}/attachments/{}?f=json&amp;amp;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!")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Also, did not have to change the headers or anything. It was always resulting in an error.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2024 18:51:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcgis-rest-api-add-attachment-issue/m-p/1205534#M65394</guid>
      <dc:creator>mikaël</dc:creator>
      <dc:date>2024-11-13T18:51:11Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Rest API Add Attachment Issue</title>
      <link>https://community.esri.com/t5/python-questions/arcgis-rest-api-add-attachment-issue/m-p/1392131#M70023</link>
      <description>&lt;P&gt;I just spent a week down this rabbit hole and your code here was the key. Thank you so much for posting this!&lt;/P&gt;&lt;P&gt;I think the params assignment should read like this though:&amp;nbsp;params = {"f": "json", "token": token}&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Again, thank you so much!&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 20:46:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcgis-rest-api-add-attachment-issue/m-p/1392131#M70023</guid>
      <dc:creator>JessicaHanscomBrunswickGIS</dc:creator>
      <dc:date>2024-03-06T20:46:46Z</dc:date>
    </item>
  </channel>
</rss>

