TransWikia.com

Details for function Script.Util.HttpResponse

Salesforce Asked on December 27, 2020

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

One Answer

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.

Answered by Gortonington on December 27, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP