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.

  1. Thanks for the great article. I have a question here – is it possible to pass the data from one Business unit Cloud Page to another BU cloud page?

  2. Thank you and yes, it is possible. All depends on the data type and what you intent to do with the data.

  3. Thanks for the great article. I have a question here – is it possible to pass the Ampscript variable to the payload string rather than a static value.
    SET @payload = ‘name=@Doctor’

  4. It is but more like this: SET @payload = CONCAT('name=',@Doctor)

  5. Hi,
    Great article!
    How do you handle special characters using the HTTPPost2 method?
    Thanks,

  6. I would put text/html; charset=utf-8 instead of just text/html.

  7. Hi, how can I use the HTTPPost2 once it executed it will redirect to the target page with the values as I can’t seem to get the values I put in the payload sadly into the target page 🙁 Thank you so much! And the article is really helpful!

  8. Hey, in the Developer Tools, have a look at the Network tab and filter by XHR Requests. You should be able to see what you’re sending and receiving. This page is about sending HTTP requests, not form-data. So if you want to redirect after the success of your page, you need to do it in the JavaScript of your Cloud page, not in the server-side JavaScript. Something like, when the HTTP request is successful, do: window.location.replace("http://www.w3schools.com");

Comments are closed.

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