6

Is there any way via AMPScript or SSJS to utilize HTTP methods outside of GET and POST?

I have had no problem utilizing these two via the HTTPPost() and HTTPGet() functions, but have not found any way to utilize any of the other methods - such as PUT, DELETE or PATCH.

The reason I ask is that I have inherited some script all in AMPscript that gathers, formats and separates the data/HTML for emails, but now we need to use PUT to update via REST API and I would rather not have to rewrite the whole thing or otherwise export this data and send to another system to run the REST call.

I also would prefer a non-clientside option as it is a major security risk - so no JavaScript/Ajax.


EDIT

I have found something that seems to hopefully point me in the right direction, but have had no luck myself yet.

The function is in SSJS as Script.Util.HttpRequest()

It shows the option to change the method, but as I said, I have not gotten this to work yet:

Additional Properties
contentType - String value indicating content type sent with requests using POST method
method - String value indicating HTTP method to use:
GET
DELETE
HEAD
OPTIONS
PATCH
POST
PUT
postData - String value indicating POST data sent with request - required for POST method

2 Answers 2

21

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.

6
  • 2
    There are a bunch of other SSJS functions that are completely undocumented, e.g. Platform.Request.RequestURL, etc. Would be nice if they'd actually support the community by releasing them. Jan 18, 2018 at 21:13
  • 1
    The HTTP properties are (as I like to say) 'Lightly Documented' here: developer.salesforce.com/docs/… Jan 18, 2018 at 21:28
  • 1
    @Gortonington you are a lifesaver. Your answer makes my day, a buddy of mine refer me here!!!!
    – tkhuynh
    Mar 29, 2018 at 23:33
  • 1
    I wish I could upvote this answer more than once Mar 24, 2020 at 19:04
  • How to pass certificate (.crt) and key file in this request?
    – anurag
    Apr 10, 2020 at 12:25
0

Unfortunately not, the only two methods are GET and POST. The SSJS and AMPScript functions are limited and can't be extended to other VERBS.

5
  • Thanks for the answer Amtera, can you please provide me some sources or documentation backing this up? I have found that just because SFMC does not list something in their docs, does not mean it is not possible. Jan 10, 2018 at 14:28
  • So the AMPScript functions, HTTPGET and HTTPPOST are pretty self explanatory - they can't be modified to support different verbs. The SSJS HTTP.Post and HTTP.Get methods are just wrappers for the equivaluent AMPScript functions. The definition for the HTTP.Get method contains the following reference. Platform.Function.HTTPGet(url, 0, 0, headerNames, headerValues, status);
    – Amtera
    Jan 10, 2018 at 14:35
  • What about the ScriptUtilHttpRequest in SSJS. It looked the most promising of all the functions (developer.salesforce.com/docs/…) Jan 10, 2018 at 14:43
  • I must've missed that function, it looks like it is achievable with Script.Util.HttpRequest() - the example on the page seems to allow you to change the method.
    – Amtera
    Jan 10, 2018 at 15:03
  • Agreed. I wanted to post and see if anyone has already done this or if there were repercussions on this function as I am sure someone else had this issue. Jan 10, 2018 at 15:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .