I'm trying to upload a .pdf file in the content of my Portal for ArcGIS with a python script. I'm trying with the "additem" REST operation. My code is as follows:

import urllib
import urllib2
import json
import argparse
import os
import json


def generateToken(username, password, portalUrl):
    '''Retrieves a token to be used with API requests.'''
    parameters = urllib.urlencode({'username' : username,
                                   'password' : password,
                                   'client' : 'referer',
                                   'referer': portalUrl,
                                   'expiration': 60,
                                   'f' : 'json'})
    response = urllib2.urlopen(portalUrl + '/sharing/rest/generateToken?',
        jsonResponse = json.loads(response)
        if 'token' in jsonResponse:
            print("Token generated OK")
            return jsonResponse['token']
        elif 'error' in jsonResponse:
            print jsonResponse['error']['message']
            for detail in jsonResponse['error']['details']:
                print detail
    except ValueError, e:
        print 'An unspecified error occurred.'
        print e

def uploadFile(token):

    '''Creates a new item in a user's content.'''    
    parameters = urllib.urlencode({'title' : 'myreport',                                    
                                   'overwrite': 'true',
                                   'type' : 'PDF',
                                   'token' : token,
                                   'async' : 'true',
                                   'multipart' : 'true',
                                   'filename' : 'newfile',
                                   'f' : 'json'
    response = urllib2.urlopen('https://xxx/arcgis/sharing/content/users/admin/additem',
    return response

sourceToken = generateToken(username='xxx', password='xxx',

itemID = json.loads(reply)["id"]

pdfFile = open('C:\\xxx\\xxx.pdf')

parameters = urllib.urlencode({
    'file' : pdfFile,    
    'partNum': '1',
    'token' : sourceToken,
    'f' : 'json'})
print(urllib2.urlopen('https://xxx/arcgis/sharing/content/users/admin/items/'+ itemID + "/addpart",parameters).read())

parameters = urllib.urlencode({
    'token' : sourceToken,
    'f' : 'json'})
print(urllib2.urlopen('https://xxx/arcgis/sharing/content/users/admin/items/'+ itemID + "/commit",parameters).read())

I have a problem with the "addpart". Here is the message :

{"code":400,"message":"Unable to add part.","details":["Invalid or missing 'file' parameter."]}

Does anyone have an idea to figure this out?

  • Is this operation doing a POST or GET? – Aamir Suleman Jan 26 '15 at 15:44
  • It does a POST :-) – ludocrn Jan 27 '15 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.