1

I was trying to fire an event from the cloud page below is the code and i am getting 500 error

<script runat=server language="JavaScript">
Platform.Load("core","1");

HTTPHeader.SetValue("Access-Control-Allow-Methods","POST");
HTTPHeader.SetValue("Access-Control-Allow-Origin","*");

if (Request.Method == "POST"){
 Variable.SetValue("@request_method", "POST");
}else{
 Variable.SetValue("@request_method", "GET");
}
</script>

%%[
Var @body, @callstatus, @response,@accessToken
IF @request_method == 'POST' THEN
 SET @token = RequestParameter('token')
  SET @email = RequestParameter('email')
  SET @is_valid_email = IsEmailAddress(@email)


      IF @is_valid_email == 'True' THEN
       SET @user_id = RequestParameter('userid')
       SET @does_user_exist = Lookup("extension lookup", "UserId", "UserId", @user_id)

       IF EMPTY(@does_user_exist) THEN
        SET @accessToken = CONCAT('Bearer ', @token)

     SET @body = '{"ContactKey": "[email protected]", "EventDefinitionKey":"APIEvent-123456789",  "EstablishContactKey": "true","Data": { "UserId":"147568","Email":"[email protected]" , "PhoneNumber":"6475552102" , "FirstName":"Karam123" , "LastName":"sialka123" , "ConfirmEmailOptIn":"true" , "ConfirmedTerms":"true" , "Language":"en-ca" , "Hash":"1455855667","link":"www.google.com"}}'   

       HTTPPost("https://subdomainofcompany.rest.marketingcloudapis.com/interaction/v1/events", "application/json", @body, @callstatus, 'Authorization',  @token)

        SET @return_msg = '{"status": "ok", "message": "event fired and Record added"}'
        Output(v(@return_msg))
       ELSE
         SET @return_msg = '{"status": "not ok", "message": "userid already exist"}'
        Output(v(@return_msg))
       ENDIF
      ELSE
       SET @return_msg = '{"status": "not ok", "message": "invalid email"}'
       Output(v(@return_msg))
      ENDIF 
    ELSE
     Redirect('google.com')
    ENDIF
    ]%%

Many thanks!

1
  • I would take your AMPscript block and replace the RequestParameter() functions with constants (string values) then paste this block in an email, preview the email for a Subscriber record and see what error is returned. Commented Apr 28, 2019 at 22:37

1 Answer 1

2

The value of @token is being passed to HTTPPost(), but that's missing the required string "Bearer " (which does appear in @accessToken). In other words, your API request should instead look like:

HTTPPost(
  "https://subdomainofcompany.rest.marketingcloudapis.com/interaction/v1/events",
  "application/json",
  @body,
  @callstatus,
  'Authorization',
  @accessToken
)

An aside: CloudPages returns a status of 500 and also mentions 500 in the body. Legacy/Classic Microsites returns a status of 200 but is a little more helpful by mentioning 401 in the body.

1
  • Hello, Mark Thanks for this! I tried with this as well and but it is not working. Commented Apr 28, 2019 at 1:31

You must log in to answer this question.

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