0

I'm looking to make HTTPPost request to send data to an endpoint from a CloudPage from within Marketing Cloud using Ampscript's HttpPost2 function based it's SFMC documentation. For some reason my Boolean variable's value is not being recognized as a true boolean but instead a string even though I am not wrapping quotes around it when I concatenate to build my payload variable.

Can you please review the code below and tell me if there is any way to insert the boolean value within the Concat function without it getting converted to a string.

Note: I posted the ampscript with a dummy api since I can't share our private endpoint

%%[

  var @optout, @requestURL, @subscriberKey, @token, @StatusCode, @payload, @httpPost2CallStatus, @httpPost2Response
 
  set @subscriberKey = RequestParameter("SubscriberKey")

  IF EMPTY(RequestParameter("optout")) THEN
     SET @optout = false
  ELSE 
     SET @optout = true
  ENDIF

  set @payload = CONCAT('{"Contact":"', @subscriberKey, '",
                   "Token":"', @token, '",
                   "optout":', @optout, '}')

  set @requestURL = "https://customOptoutApi"

  set @contentType = "application/json"

  set @StatusCode = HTTPPost2(@requestURL, @contentType, @payload, @httpPost2CallStatus, 
  @httpPost2Response)

]%%

payload: %%=v(@payload)=%%
<br><br>
response: %%=v(@httpPost2Response)=%%

For output values...here is what I see below including the payload and the response. It appears that the "F" is from the False value which appears to have been converted to a String from the Concatenation.

payload: {"Contact":"0022h00000jz48eZZZ", "Token":"42b95da510cbb3f8b4c874053a298644 q", 
"optout":False}

response: [{"errorCode":"APEX_ERROR","message":"System.JSONException: Unexpected character 
('F' (code 70)): expected a valid value (number, String, array, object, 'true', 'false' or 
'null') at [line:3, column:54]\n\n(System Code)\nClass.ContactMobileUserMD5.getContact: line 
11, column 1"}]

1 Answer 1

1

Going from the API response to the letter, it says it wants 'true'or 'false' to indicate boolean - give it that. Your question is aimed at "becoming more boolean" - you cannot and there apparently is no need. The API in the error response literally asks for quotation marks around true or false - and it does not treat that as String, otherwise it wouldn't list it next to String.

(number, String, array, object, 'true', 'false' or 'null')

Note how e.g. false is not in the list.

So I would try to just define your pseudo "boolean" variables as string:

IF EMPTY(RequestParameter("optout")) THEN
    SET @optout = 'false' /* instead of false */
ELSE 
    SET @optout = 'true' /* instead of true */
ENDIF
1
  • That did the trick! I thought I tried that earlier and it failed but it's possible I had another error someplace that I later corrected. Thank you for your quick response. Apr 6, 2022 at 13:27

You must log in to answer this question.

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