Select to view content in your preferred language

Upload sd file to server with rest api

1823
3
12-12-2019 06:12 PM
forestknutsen1
MVP Regular Contributor

I am trying to do the pattern outlined in this post: 

arcpy - Publishing service using service definition file (sd) through the ArcGIS REST API? - Geograp... 

and the same post on geonet:

https://community.esri.com/message/882685-re-how-do-i-publish-a-service-using-a-service-definition-f... 

But I am stuck on step one upload sd file to the server with rest api.

Upload—ArcGIS REST API: Services Directory | ArcGIS for Developers 

action = 'uploads/upload'
url = "http://{}:{}/arcgis/admin".format(server, port)
requestURL = url + "/{}".format(action)
query_dict = {
    'file': r'C:\xx\xx\xxx.sd',
    "token": token,
    "f": "json"
}
query_string = urllib.urlencode(query_dict)
r = urllib.urlopen(requestURL, query_string)
j = json.loads(r.read())
print j

The json output is:

{u'status': u'error', u'code': 500, u'messages': [u'This operation must be a multipart request.']}

I have also tried with requests without any luck:

token = get_token('user', 'password', 'server', '6080', 60)
item = r'C:\xx\xx\xxx.sd'
files = {'file': item}
payload = {'f': 'json', 'token': token}
url = 'http://server:6080/arcgis/admin/uploads/upload'

resp = requests.post(url, files=files, data=payload,
                     verify=False)  # Use verify=False if you don't have a certificate to use
json_data = resp.json()

If I look at the resp object in the debugger I see:

content = {"status":"error","messages":["No file is associated with the upload request."],"code":500}

How can I get this working?

Tags (1)
0 Kudos
3 Replies
FelixArnet
New Contributor

I was also trying to upload a file, however a serverobjectextension-file (SOE) and with c#.

I finally found a solution:

- I had to add ContentType "application/x-zip-compressed" to the header of the file content.

- The file-Parameter had to be named "itemFile" instead of "file" !!!!!

The relevant code part:

            using (FileStream stream = new FileStream(soePath, FileMode.Open))
            using (HttpClient client = new HttpClient())
            {
                MultipartFormDataContent content = new MultipartFormDataContent();
                content.Add(new StringContent("pjson"), "f");
                content.Add(new StringContent(token), "token");
                content.Add(new StreamContent(stream)
                {
                    Headers =
                    {
                        ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-zip-compressed")
                    }
                }
                , "itemFile", soePath);
                Task<HttpResponseMessage> task = client.PostAsync($"{Service}/admin/uploads/upload", content);

Hope that helps.

forestknutsen1
MVP Regular Contributor

Thanks Felix!

What an annoying gotcha!

0 Kudos
SiddharthaSaxena
New Contributor

Hi, If it is possible can you please share your python script. I am trying to upload soe but not been able to do.

0 Kudos