Been going around in circles for some time now.
My issue:
I submit the thing using a postman-like interface, get a 200 response and find an empty file in the filestore at the end of it.
Incredibly frustrating.
Do I have to program the thing myself to pick up the file from wherever it is; move it into the filestore and register it, or is there a bit of the REST API I’m missing out on?
Would love to know.
This Workflow script uploads the current data file to the file store (regardless of its format) and stores the resulting Data File ID in JobInfo 8:
var connectInfo = JSON.parse( Watch.GetConnectToken() );
var baseURL = "http://" + connectInfo.host + ":" + connectInfo.port;
var fileID = uploadFile(Watch.GetJobFileName(),connectInfo.token);
Watch.SetJobInfo(8,fileID);
Watch.Log(fileID,2);
function uploadFile(fName, token){
var uploadDataFile = "/rest/serverengine/filestore/DataFile?persistent=true&filename="+Watch.ExpandString("%U");
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
xhr.open("POST", baseURL + uploadDataFile, false);
xhr.setRequestHeader("auth_token", token);
xhr.setRequestHeader("Content-Type", "application/octet-stream");
var myStream = readBinaryFile(fName);
xhr.setRequestHeader("Content-Length",myStream.Size);
xhr.send(myStream.Read());
myStream.Close();
return xhr.responseText;
}
function readBinaryFile(fileName){
var adTypeBinary = 1;
var binaryStream = new ActiveXObject("ADODB.Stream");
binaryStream.Type = adTypeBinary;
binaryStream.Mode = 3;
binaryStream.Open();
binaryStream.LoadFromFile(fileName);
return binaryStream;
}
You can then use JobInfo 8 in a standard File Store - Download task to retrieve the file, from the file store.
As you can see from that bit of script, the file is uploaded with its content-type
set to application/octet-stream
, to make sure no transformations are applied to the file at either end of the communication. The file is essentially processed as a binary entity.
Hopefully, that will help you get going.
In Visual Studio Code’s Thunder client (which is almost identical to PostMan), it looks like this:
I manually encoded my username:password
in Base64 to set the Authorization
header for the call.
Hi Phil,
Thanks for getting back to me so quickly and the code tips.
We are investigating the option of leaving everything ‘Workflow’ related behind and interacting with Connect solely through the REST API.
Would you mind letting me know if there are any obvious gotchas in the js above (we’ll be using either Python or Java, so I have a bit of translation to do as it is).
Last thing for now: once in the filestore, I’m guessing that datamappers etc will have access directly rather than me having to tell them to download / process / put back in the box for later?
Thanks again,
T
I think the only shortcut that the above JS code is taking relates to authentication: it uses Workflow’s Watch.GetConnectToken()
method, which natively fetches a new token that’s used for subsequent operations. Since you won’t be using Workflow, you’ll have to use the Connect REST API’s Authenticate/Login method to get that token manually.
Note also that the code above sets the file to persistent, which means the file will remain in the FileStore until you manually delete it. If you don’t specify the persistent attribute, then the file will eventually be deleted by the cleanup service (it only gets deleted if no data record depends on it).
Other than that, the code is pretty straightforward.
And yes, once the file is uploaded to the FileStore, you can use its Managed File ID for any data mapping method that needs to use that file as input data.
Perfect,
Thanks ever so much Phil.
Very happy.
All the best,
T
All working fine.
Small GDPR issue on the horizon though, and was wondering if there was any way of changing the filestore directory or is that baked in to the API?
Many Thanks,
T
No, it is not currently possible to change the location of the Filestore. The feature is in our backlog, however, so don’t lose hope yet!
Thanks Phil - some behind-the-scenes hot folders then. Not a biggy.