0

I am trying to send data with SSJS to an API but am not finding the error. I always receive the 500 error when publishing the cloudpage to test it.

<script runat="server">
Platform.Load("Core", "1.1.1");

// Define the URL
var url = "https://domain.de/be/api/v2/apply/referral/";

// Define the JSON payload
var payload = {
  "tenant_id": 109,
  "email": "[email protected]",
  "first_name": "Name",
  "last_name": "Name",
  "zipcode": "35334",
  "phone": "453-261-3853"
};

 // Convert the payload to JSON format
var jsonPayload = Platform.Function.Stringify(payload);

// Define the Bearer token
var bearerToken = "sxsxs.scsddscsdcsdcsdcsd";

// Set the headers with the Bearer authorization
var headers = {
  "Authorization": "Bearer " + bearerToken,
  "Content-Type": "application/json"
};

// Send the HTTP POST request with the headers
var response = HTTP.Post(url, null, jsonPayload, headers);

// Check the response status
if (response.StatusCode == 201) {
  Write("JSON sent successfully!");
} else {
  Write("Failed to send JSON. Status code: " + response.StatusCode);
}

</script>

1 Answer 1

2

I see the following:

a) Wrap a try/ catch around the HTTP.POST for debugging / to get rid of generic 500s.

b) contentType needs to be the second argument, not sent as header

c) headernames and values are two separate arguments into which you enter arrays.

https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_httpPost.html

      var contentType = 'application/json';
      var headerNames = ["Authorization"];
      var headerValues = ["Bearer " + bearerToken];
      try {
      var result = HTTP.Post(endpoint,contentType,payload,headerNames,headerValues);
      } catch (e) {
          Write(Stringify(e));
      }
 
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .