<?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: Correct syntax for �??RESTful�?? addAttachment? in ArcGIS REST APIs and Services Questions</title>
    <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/correct-syntax-for-restful-addattachment/m-p/186067#M880</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;When the documentation is insufficient to figure out how to write code I use Fiddler in order to see how ESRI does it and then try to replicate the same.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That said, the documentation you linked states the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"This operation adds an attachment to the associated feature (POST only). The addAttachment operation is performed on a feature service feature resource.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Since this request uploads a file, it must be a multipart request pursuant to IETF RFC1867."&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am pretty sure that this is telling you that you have to literally embed the bits from the file in the body of the POST according to the referenced standard.&amp;nbsp; I suspect that there is a Python library which provides this but I can't help you there.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 08 Apr 2014 21:22:12 GMT</pubDate>
    <dc:creator>RichardWatson</dc:creator>
    <dc:date>2014-04-08T21:22:12Z</dc:date>
    <item>
      <title>Correct syntax for ???RESTful??? addAttachment?</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/correct-syntax-for-restful-addattachment/m-p/186066#M879</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I�??m working on a geoprocessing script that uploads a jpg from a local drive to a feature service.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I�??m having troubles getting the operation syntax right for posting the �??addAttachment�?? request to the Server.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Link to online resources:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#/Add_Attachment/02r3000000wt000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#/Add_Attachment/02r3000000wt000000/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I�??ve tried the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1)&amp;nbsp; addAttachment via the ArcGIS server web browser interface (WORKING)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;response:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{"addAttachmentResult":{"objectId":2001,"success":true}}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2)&amp;nbsp; I tried to replicate #1 by entering the following request into the URL bar in a web browser (NOT WORKING):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;A class="jive-link-external-small" href="http://" rel="nofollow noopener noreferrer" target="_blank"&gt;http://&lt;/A&gt;&lt;SPAN&gt;&amp;lt;my-server&amp;gt;/arcgis/rest/services/GeoProcessing_tools/Milling_Statement_points/FeatureServer/0/1602/addAttachment?f=json&amp;amp;attachment=C:\TEMP\pic00002.jpg&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;response:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;{"error":{"code":500,"message":"java.lang.String cannot be cast to com.esri.arcgis.discovery.json.JSONObject","details":[]}}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My attachment parameter is obviously not correct. What would be the correct syntax?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The online resources�?? only info on this was: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;�??The input parameter attachment to this operation is a file.�?�&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3) Lastly, I tried to post the information as part of my python script using urllib (NOT WORKING):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
Import urllib
url=�?? http://&amp;lt;my-server&amp;gt;/arcgis/rest/services/GeoProcessing_tools/Milling_Statement_points/FeatureServer/0/1602/addAttachment�??
payload = {'f':'json', 'attachment':{'url': r'file:///C:/TEMP/pic00002.jpg', 'contentType' : 'image/jpg'}}
response = urllib.urlopen(url, urllib.urlencode(payload)).read()
print response
{"error":{"code":500,"message":"JSONObject[\"uploadedFilePath\"] not found.","details":[]}}
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Both error messages (2 &amp;amp; 3) include �?? �??code�?? : 500�?� which appears to point to a server-side problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;However, the addAttachment operation works when using the web browser interface (1). Does that not contradict a server-side issue?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I�??d appreciate any help on this problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Katrin&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:25:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/correct-syntax-for-restful-addattachment/m-p/186066#M879</guid>
      <dc:creator>KatrinSattler</dc:creator>
      <dc:date>2021-12-11T09:25:30Z</dc:date>
    </item>
    <item>
      <title>Re: Correct syntax for �??RESTful�?? addAttachment?</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/correct-syntax-for-restful-addattachment/m-p/186067#M880</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;When the documentation is insufficient to figure out how to write code I use Fiddler in order to see how ESRI does it and then try to replicate the same.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That said, the documentation you linked states the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"This operation adds an attachment to the associated feature (POST only). The addAttachment operation is performed on a feature service feature resource.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Since this request uploads a file, it must be a multipart request pursuant to IETF RFC1867."&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am pretty sure that this is telling you that you have to literally embed the bits from the file in the body of the POST according to the referenced standard.&amp;nbsp; I suspect that there is a Python library which provides this but I can't help you there.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Apr 2014 21:22:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/correct-syntax-for-restful-addattachment/m-p/186067#M880</guid>
      <dc:creator>RichardWatson</dc:creator>
      <dc:date>2014-04-08T21:22:12Z</dc:date>
    </item>
    <item>
      <title>Re: Correct syntax for �??RESTful�?? addAttachment?</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/correct-syntax-for-restful-addattachment/m-p/186068#M881</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the quick response, Richard! &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Both valuable pointers, thanks a lot. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Fiddler indeed revealed the multipart MIME request syntax.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I assume that's the way to go.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 Apr 2014 01:22:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/correct-syntax-for-restful-addattachment/m-p/186068#M881</guid>
      <dc:creator>KatrinSattler</dc:creator>
      <dc:date>2014-04-14T01:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: Correct syntax for ???RESTful??? addAttachment?</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/correct-syntax-for-restful-addattachment/m-p/186069#M882</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;To complete this post, here my solution to the originally posted problem:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14059979769915552" jivemacro_uid="_14059979769915552" modifiedtitle="true"&gt;
&lt;P&gt;Import requests&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;url =r'&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://" rel="nofollow" target="_blank"&gt;http://&lt;/A&gt;&lt;SPAN&gt;&amp;lt;my-server&amp;gt;/arcgis/rest/services/GeoProcessing_tools/Milling_Statement_points/FeatureServer/0/1602/addAttachment'&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;imageRaw = r'C:/TEMP/pic00006.jpg'&lt;/P&gt;
&lt;P&gt;imageBin = open(imageRaw, 'rb')&lt;/P&gt;
&lt;P&gt;files = {'attachment': ('pic00006.jpg', imageBin, 'image/pjpeg')}&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;response = requests.post(url, files = files)&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The solution was to convert the image into a binary file (thanks Richard).&lt;/P&gt;&lt;P&gt;The response is in html and can be parsed by using &lt;EM&gt;BeautifulSoup &lt;/EM&gt;and&lt;EM&gt; ast &lt;/EM&gt;for error handling:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14059988877234746" jivemacro_uid="_14059988877234746" modifiedtitle="true"&gt;
&lt;P&gt;#Parse html response&lt;/P&gt;
&lt;P&gt;markup = BeautifulSoup(response.text)&lt;/P&gt;
&lt;P&gt;messageRaw = markup.pre.string&lt;/P&gt;
&lt;P&gt;message = ast.literal_eval(messageRaw.replace("true", " 'true' "))&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;#Confirm success &lt;/P&gt;
&lt;P&gt;if message['addAttachmentResult']['success'] == "true":&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("image was successfully attached to feature")&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;else:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddWarning("image was NOT successfully attached; server response: " + messageRaw.replace("\n", ""))&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Having to replace strings and line breaks is obviously not a very clean solution.&lt;/P&gt;&lt;P&gt;I did not succeed in requesting the response in json format or in converting the html response into json, which would be easier to parse.&lt;/P&gt;&lt;P&gt;Any tips on either are still welcomed.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Jul 2014 03:28:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/correct-syntax-for-restful-addattachment/m-p/186069#M882</guid>
      <dc:creator>KatrinSattler</dc:creator>
      <dc:date>2014-07-22T03:28:21Z</dc:date>
    </item>
  </channel>
</rss>

