VMware Cloud Community
mobcdi
Enthusiast
Enthusiast

Working with ResourceElement

Is it possible to update a ResourceElement with the string content from a http-rest get request?

I want to download a file, compare it to the current resourceElement and if downloaded file differs from resourceElement then update the resourceElement with the downloaded content.If possible I would also like to increment the versions at the same time

Tags (2)
0 Kudos
4 Replies
Jalapeno420
Enthusiast
Enthusiast

I have not played with REST but below is how you can read and write to a ResourceElement.  I did not see a way to change the version, but I am still learning.

re = your ResourceElement object

Reading

attachment = re.getContentAsMimeAttachment() ;

stringContents = attachment.content;

Writing (overwrites)

attachment = new MimeAttachment;

attachment.content = "Some String";

attachment.mimeType = "text/plain";

re.setContentFromMimeAttachment(attachment);

0 Kudos
mobcdi
Enthusiast
Enthusiast

Thanks I will give it a go

0 Kudos
mobcdi
Enthusiast
Enthusiast

I'm still learning myself.

I created a workflow that takes in an existing resource element and the content to save to it but after running the workflow the content isn't getting saved into the resource element itself.

in a scriptable task I have the following, the Resource is listed as an input (bound to the ResourceElement I want to update) in the scriptable task and the output of the scriptable task is a boolean

// Load the content into the attachment
try {
    attachment.content = StringUpdate;
} catch(e) {
    throw e.message;
}
//Load the attachment into the resource
try {
    System.log("Attempting to save new content to: "+ Name);
    Resource.setContentFromMimeAttachment(attachment);
    Response = true;
} catch(e) {
    throw e.message;

Does the mimecontent type of the attachment have to match the type set for the resource element when it was created or do I need to at least set the mimecontent to a value when I load the attachment?

Or is it down to a more basic coding problem I'm missing

0 Kudos
maverix7
VMware Employee
VMware Employee

You should set mimeType on the mime attachment. If the content is text, you should set "text/plain", if you don't know the content it will be best to use "application/octet-stream", that stands for binary files.

so you can update your code with :

try {

    attachment.content = StringUpdate;
    attachment.mimeType = "text/plain";    
} catch(e) {
    throw e.message;
}

As you want to update the resource element, you can also specify the mimeType of the original element.

attachment.mimeType = resourceElement.mimeType; 

0 Kudos