2

Can someone provide example code for the function Script.Util.HttpResponse or point to a better resource than these help docs?

1 Answer 1

6

As a note, Script.Util.HttpResponse is not a user function, it is instead the Response Object from the other Script.Util calls.

For details to correctly parse information from it, I would look at my article on Script.Util.HttpRequest about how to handle the Response from the call (Parsing the Results). This discusses how to grab information from the returned object of an HttpRequest call which is, in actuality, the Script.Util.HttpResponse object as documented in the official docs.

As shown in the documents, the following parameters are available from the object:

  1. content - String value containing HTTP response content returned from URL
  2. contentType - String value indicating content type returned by response
  3. encoding - String value indicating encoding returned by response
  4. headers - Object containing HTTP response header collection returned from response
  5. returnStatus - Integer value containing the Marketing Cloud response to the request:
  • 0 - OK
  • -1 - Empty URL
  • -2 - Call failed
  • -3 - Call succeeded with empty content
  1. statusCode - Integer value containing the HTTP response status code returned from URL, such as 200, 404, or 500

You would use these like below:

//assuming var res is the response object from an HttpRequest call
var content = res.content; 
var contentType = res.contentType;
var encoding = res.encoding;
var headers = res.headers["myHeaderName"];
var status = res.returnStatus
var code = res.statusCode

A couple notes though, res.content is returned as a CLR object, which is not really compatible with SFMC SSJS, so you will need to convert it to a string and then use ParseJSON to turn it into a recognizable object/array to output.

Like below:

var resStr = String(res.content);
var resJSON = Platform.Function.ParseJSON(resStr);

NOTE: If you use Stringify instead, this will not work. You need to change the data type of the object to String or it will not output correctly.From the String version of the JSON you can then parse that into a JSON that you can interact with.


NOTE ON HEADERS:
EDIT 2020-11-09 - Based on info grabbed in this thread if you are getting errors about "Use of Common Language Runtime (CLR) is not allowed" when working with the headers, you will need to talk to support to turn on an associated Business Rule to allow this.


All this being said, there may be some other quirks or options on this object that I am unaware of as I have not done too extensive research into this. Please feel free to explore and add any comments here if you find anything cool.

3
  • How do you find the keys of the JSON object? When I write the JSON object to the CloudPage, it only shows the values. Commented Sep 7, 2022 at 3:20
  • @NickNelson - what do you mean? When you do something like Write(Stringify(resJSON)) it provides an object without Keys? because if so, that sounds malformed and should instead be an array. I would check with the endpoint and see what the expected return is and why that is not what you are receiving. Commented Sep 7, 2022 at 13:07
  • 1
    Yes, that's what was happening. I figured it out. It was returning an XML content-type instead of json, so I just had to tweak the headers a bit. Thanks for the response. Commented Sep 7, 2022 at 20:00

You must log in to answer this question.

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