How to send an HTTP request from one Marketing Cloud page to another

How to send an HTTP request from one Marketing Cloud page to another

This article explains how to send an HTTP request with AMPscript and server-side Javascript from one Marketing Cloud page to another and display the response.

Not a fan of reading? Jump to the code snippet.

Send a request

For the purpose of this exercice, let’s create 2 cloud pages, make the first page send an HTTP request to the target page and, if successful, display the response.

AMPscript

In AMPscript, there are several functions available, but in this case we are going to use HTTPPost2, which is the most advanced.

Instead of working with URLs, we are going to use the Page ID, which can be found in the Page Properties, and the CloudPagesURL function.

In this example, the Page ID of the target page will be 1234.

Note that in order to get a proper response from the target Cloud page, we need to include the header Accept-Encoding with the value identity.

%%[
	SET @req = HTTPPost2(CloudPagesURL(2181),'text/html', '', true, @resp, @respheader, 'Accept-Encoding', 'identity')
    IF @req == 200 THEN
		OUTPUTLINE(CONCAT('HTTP request was successful'))
    ELSE
    	OUTPUTLINE(CONCAT('HTTP request failed'))
    ENDIF
]%%

Server-side Javascript

In server-side Javascript, the request can be sent in a similar fashion. The downside is: there is no CloudPagesURL function and we need to use the full URL to send the request.

<script runat="server">
    Platform.Load("Core", "1.1.1")
    var req = HTTP.Post('https://yourmcdomain.com/target-page', 'text/html', '', ['Accept-Encoding'], ['identity']);
  	if(req.StatusCode == 200) {
       Write('HTTP request was successful');
    } else {
       Write('HTTP request failed');
    }
</script>

Write a response

Let’s write a script on the target page that expects a parameter called name and returns the appropriate response.

AMPscript

In AMPscript, capturing the data from a POST request is a matter of using the RequestParameter function.

%%[
	SET @Name = RequestParameter("name")
	IF @Name == "Doctor" THEN
		OUTPUT(CONCAT("Who?"))
	ENDIF
]%%

Server-side Javascript

In SSJS, the RequestParameter function equivalent is Request.GetFormField and works in the same manner.

<script runat="server">
    Platform.Load("Core", "1.1.1")
    var name = Request.GetFormField("name");
    if(name == 'Doctor') {
       Write('Who?');
    }
</script>

Display the response

Now, we need to adapt the script on the first page to send an URL parameter and display the response from the target page.

AMPscript

In order to pass parameters as if they were in the URL, the content type application/x-www-form-urlencoded is required.

%%[
	SET @payload = 'name=Doctor'
 	SET @req = HTTPPost2(CloudPagesURL(2181),'application/x-www-form-urlencoded', @payload, true, @resp, @respheader, 'Accept-Encoding', 'identity')
    IF @req == 200 THEN
  		OUTPUTLINE(CONCAT(@resp))
    ELSE
     	OUTPUTLINE(CONCAT('HTTP request was unsuccessful'))
    ENDIF
]%%

Server-side Javascript

We can do the same with SSJS.

<script runat="server">
    Platform.Load("Core", "1.1.1")
	var payload = 'name=Doctor';
    var req = HTTP.Post('https://yourmcdomain.com/target-page', 'application/x-www-form-urlencoded', payload, ['Accept-Encoding'], ['identity']);
  	if(req.StatusCode == 200) {
       Write(String(req.Response));
    } else {
       Write('HTTP request was unsuccessful');
    }
</script>

Working with structured data

We can also use this method to send and return a JSON object. The key is to convert the object into a String and pack it in a URL parameter.

First page

<script runat="server">
	Platform.Load("Core", "1.1.1")
	var data = {
		'name' : 'Doctor'
	}
	var payload = "data=" + Stringify(data);
	var req = HTTP.Post('https://yourmcdomain.com/target-page', 'application/x-www-form-urlencoded', payload, ['Accept-Encoding'], ['identity']);
	if(req.StatusCode == 200) {
		var resp = Platform.Function.ParseJSON(String(req.Response));
		Write(resp.Message);
	} else {
		Write('HTTP request was unsuccessful');
	}
</script>

Target page

<script runat="server">
    Platform.Load("Core", "1.1.1")
    var data = Request.GetFormField("data");
  	data = Platform.Function.ParseJSON(data);
    if(data.name == 'Doctor') {
       var resp = {
       		'Message' : 'Who?'
       }
       Write(Stringify(resp))
    }
</script>

Have I missed anything?

Please poke me with a sharp comment below or use the contact form.

Pay me a coffee

Want to say thanks? Pay me a coffee! Remember, I turn coffee into code.

Leave a Reply

Your email address will not be published. Required fields are marked *

server-side Javascript
Up Next:

How to retrieve records using complex filters with server-side Javascript

How to retrieve records using complex filters with server-side Javascript