Fusion Manage Forum
Welcome to Autodesk’s Fusion Manage (formerly Fusion 360 Manage) Forum. Share your knowledge, ask questions, and explore popular Fusion Manage topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

API v1 Uploading Attachment with Python

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
tgoryukULDWM
769 Views, 2 Replies

API v1 Uploading Attachment with Python

I'm able to login and request data but when attempting to upload an attachment the server responds with an error 400.

 

The upload code in python 3.7 using the requests module.

 

def uploadAttachment(workspace, dmsID):
    global tenant
    global cookie 

    url = f'https://{tenant}.autodeskplm360.net/api/rest/v1/workspaces/{workspace}/items/{dmsID}/attachments'

    headers = {'Cookie': cookie,
               'Content-Type': 'multipart/mixed',
               'Accept': 'application/json'}
files = { 'metadata': ('input_payload_file.json', open('input_payload_file.json', 'rb'), 'application/json'), 'content' : ('name.ext', open('dog.jpg', 'rb'), 'application/octet-stream') } req = requests.post(url, headers=headers, files=files) if req.ok: print(req.text) else: print(req.status_code)

I've inspected the post request being sent with netcat running locally and it seams to match the curl example provided in the API documentation.

 

POST / HTTP/1.1
Host: localhost:8080
User-Agent: python-requests/2.21.0
Accept-Encoding: gzip, deflate
Accept: application/json
Connection: keep-alive
Cookie: customer=removed;JSESSIONID=removed
Content-Type: multipart/mixed
Content-Length: 4531

--1cc69beceb8e25134176cf57f9c3ff10
Content-Disposition: form-data; name="metadata"; filename="input_payload_file.json"
Content-Type: application/json

{
   "fileName":"dog.jpg",
   "resourceName":"ProductPhoto01",
   "description":"dog dog dog."
}
--1cc69beceb8e25134176cf57f9c3ff10
Content-Disposition: form-data; name="content"; filename="name.ext"
Content-Type: application/octet-stream

ÿØÿà...........................(file data removed)...............

--1cc69beceb8e25134176cf57f9c3ff10--

 

 

 

2 REPLIES 2
Message 2 of 3

Uploading attachments is difficult, the format has to be just right. Here is my python script to upload an attachment. You will need to modify your JsessionID, URL, File paths and file names but other than that it should work.

import requests
import json

url = 'https://[YourTenant].autodeskplm360.net/api/rest/v1/workspaces/[YourWorkspace]/items/[DMSID]/attachments'

cookies = {
    'JSESSIONID': '[JSESSIONID]',
}

headers = {
    'Content-Type': 'multipart/form-data',
    'Accept': 'application/json',
}

payload = {"fileName":"Test.jpg","resourceName":"File size test","description":"photo upload from Python"}

multiple_files = {
        'json': (None, json.dumps(payload), 'application/json'),
        'file': ('IMG 4768.jpg', open('[Filepath]/IMG_4768.jpg', 'rb'), 'application/octet-stream')
        }

response=requests.request("POST", url, cookies=cookies, files=multiple_files)


print(response.text)

 

Message 3 of 3

Thank you so much that worked perfectly!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report