V1 API Creating an Attachment

V1 API Creating an Attachment

Anonymous
Not applicable
706 Views
3 Replies
Message 1 of 4

V1 API Creating an Attachment

Anonymous
Not applicable

Hi,

 

I am having some difficulty uploading a file using the V1 api? Has anyone got a working script that they can share with me in PHP or cURL? I'm interested also in what form the metadata file needs to take.

 

Thanks

 

David.

0 Likes
707 Views
3 Replies
Replies (3)
Message 2 of 4

gasevsm
Alumni
Alumni

Hi David,

 

To attach a file to a given item, the call needed is as follows:

 

    POST api/rest/v1/workspaces/{id}/items/{dmsID}/attachments

 

How to about doing this? Well, you must create the above HTTP request with type of multipart/mixed or multipart/form-data and provide the metadata and binary information into two parts within the same call.

The first part should be declared as application/json and provide a name, unique identifier, and description to use for the file in PLM.

* The second provides the binary stream of the file being uploaded. Its content would be declared as application/octet-stream.

 

There is a basic cURL example here that unfortunately, doesn't cover the binary stream section.

http://help.autodesk.com/view/PLM/ENU/?guid=GUID-F31EF246-A5EA-4892-B188-AA3F11DE4DBB

 

Here's an example for each part in that POST multipart request payload:

 

// part 1

  Content-Type: application/json
  {
  "fileName":"MyFile.jpg",
  "resourceName":"ProductPhoto01",
  "description":"Front cover photo for product."
  }

 

// part 2

  Content-Type: application/octet-stream
  Content-Disposition: *; filename=name.ext;
  {binary-number}
  {binary-number}
  ...

 

 

You should get a response similar to this:

{
  "file": {
  "deleted":false,
  "dmsID":297,
  "versionID":0,
  "workspaceID":8,
  "dmsID":297,
  "fileID":580,
  "fileName":"MyFile.jpg",
  "resourceName":"ProductPhoto01",
  "description":"Front cover photo for product.",
  "fileSize":108315,
  "fileVersion":1,
  "fileStatus": {
  "statusID":1,
  "status":"Checked IN",
  "description":"The file is checked in and available for check out"
  },
  "createdUserID":"jbloggs",
  "createdDisplayName":"Joe Bloggs",
  "timeStamp":"2014-08-01T15:58:51.688-04:00",
  "folderId":0
  }
}

 

 

Hope this helps,


Martin Gasevski | Fusion 360 Team Product Manager
0 Likes
Message 3 of 4

Anonymous
Not applicable

Thanks Martin. I'll check that out now.

0 Likes
Message 4 of 4

Anonymous
Not applicable

Hi Martin,

 

I was able to finally implement this is PHP. Thanks for your help. I am reposting the script here in case anyone else finds it useful. In my example I am trying to create an attachment using a file called 'test.jpg' that's store on my local hard-drive.

 

<?php

 

require_once [path to PLM PHP login script];
require_once [path to PLM PHP logout script];

 

//Log in to PLM
$sessionid = login();

$body_data = '{"fileName":"test.jpg","resourceName":"Test","description":"This is a test photo."}';
$body_file = file_get_contents("C:\test.jpg");

$BODY = array("metadata" => $body_data, "content" => $body_file);

//Curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data","Cookie: JSESSIONID={$sessionid}" ));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, "https://[tennant].autodeskplm360.net/api/rest/v1/workspaces/[ws id]/items/[dmsid]/attachments");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY);
$result = curl_exec($ch);
curl_close($ch);

logout($sessionid);

?>

 

 

0 Likes