Details for function Script.Util.HttpResponse

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

Salesforce Asked on December 27, 2020

1 Answers

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.

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!

Related Questions

Apex Regex Extract Numeric Value from String

3  Asked on January 6, 2022 by yossi-cadaner


Ignore files when force:source:convert

4  Asked on January 6, 2022 by simonp


AuthToken using Open Id Connect

1  Asked on January 6, 2022 by bergin-panimayam


Using IsPickVal() function inside Email Templates

1  Asked on January 6, 2022 by anthony-williams


Create record by submitting string for reference field

1  Asked on January 4, 2022 by user85715


Ask a Question

Get help from others!

© 2022 All rights reserved.