We need to retrieve the response headers from an HTTP GET call issued in SSJS with the Script.Util.HttpRequest function.
Unfortunately, even though it seems straightforward and the task itself is pretty trivial, we cannot get our head around it.
We already read the answer by (the great) Gortonington to this very recent thread, but event though it shed some light, it does not solve our problem.
We understand that header are returned as CLR objects, but evidently we are doing something wrong.
Below is the (one of the many) snippet of code that clarifies what we are trying to achieve:
<script runat="server" type="text/javascript">
Platform.Load("core","1");
var endpoint = "www.google.com";
var req = new Script.Util.HttpRequest(endpoint);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.method = "GET";
req.setHeader("Cache-Control","no-cache");
var resp = req.send();
try {
Platform.Response.Write("Return header Stringify: " + Stringify(resp.headers) + "<br />");
Platform.Response.Write("Return header: " + resp.headers["server"] + "<br />");
var resultJSON = Platform.Function.ParseJSON(String(resp.headers["server"]));
Platform.Response.Write("Return header ParseJSON: " + Stringify(resultJSON));
} catch(e) {
Write(Stringify(e));
}
</script>
Inside the try block there are some attempts to print the "server" header (we also tried "Server", "location" and others). The first Stringify returns an empty object. The other two attempts return: "Use of Common Language Runtime (CLR) is not allowed".
Can anyone help us on this matter? Thanks in advance.
resp.headers["Server"]
it should provide you a value.var resultHeaderStr = resp.headers["Server"];
. If you are actually sending to Google (like you show in your example) this is likely the issue. Try hitting an endpoint like:https://httpbin.org/status/200