REST API Methods in AMPScript or SSJS
GREAT NEWS!!!
Firstly I want to thank SFMC Global Support, for the first time since they were Salesforce they were actually EXTREMELY helpful and quick. (Shout out to Jeff Ross)
We found out that with the Script.Util.HttpRequest() function you actually CAN use
GET
DELETE
HEAD
OPTIONS
PATCH
POST
PUT
It took a few days of both of us digging and searching, but we found it. Hope this helps someone else avoid this headache!
Below is a sample script for this:
<script runat=server>
Platform.Load("core", "1.1.1");
var accessToken = {{yourToken}};
var url = 'https://www.exacttargetapis.com/asset/v1/content/assets/{{ContentID}}'
var payload = '{{yourPayload}}';
var auth = 'Bearer ' + accessToken;
var req = new Script.Util.HttpRequest(url);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.contentType = "application/json"
req.setHeader("Authorization", auth);
req.method = "PUT"; /*** You can change the method here ***/
req.postData = payload;
var resp = req.send();
</script>
EDIT - Adding in some info on parsing the results (also is on my blog post in more details)
First thing to note is that the results are returned in a Script.Util.Response object which is in a CLR format. Without going into details, pretty much this means that the current result is something that you cannot interact with directly.
So first things first, we need to change the content returned from CLR to a String. Luckily there is a native Javascript function to do this. String()
.
var resultString = String(result.content)
As a string, the results are not easily parsed or dissected to gather the info, but there is another function in SSJS that we can use – ParseJSON()
.
See below for sample of turning the String into a JSON
var resultJSON = Platform.Function.ParseJSON(String(result.content));
This will then store the content from the results of the request inside of a JSON, which is easily interacted with via SSJS. From here you can treat this result the same as other arrays or objects and request info accordingly.