Import urllib url=�?? http://<my-server>/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":[]}}
To complete this post, here my solution to the originally posted problem:
Import requests
url =r'http://<my-server>/arcgis/rest/services/GeoProcessing_tools/Milling_Statement_points/FeatureServer/0/1602/addAttachment'
imageRaw = r'C:/TEMP/pic00006.jpg'
imageBin = open(imageRaw, 'rb')
files = {'attachment': ('pic00006.jpg', imageBin, 'image/pjpeg')}
response = requests.post(url, files = files)
The solution was to convert the image into a binary file (thanks Richard).
The response is in html and can be parsed by using BeautifulSoup and ast for error handling:
#Parse html response
markup = BeautifulSoup(response.text)
messageRaw = markup.pre.string
message = ast.literal_eval(messageRaw.replace("true", " 'true' "))
#Confirm success
if message['addAttachmentResult']['success'] == "true":
arcpy.AddMessage("image was successfully attached to feature")
else:
arcpy.AddWarning("image was NOT successfully attached; server response: " + messageRaw.replace("\n", ""))
Having to replace strings and line breaks is obviously not a very clean solution.
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.
Any tips on either are still welcomed.