0

I am trying to take all responses from Mobile Connect and dump them into a third party resource for analysis via an ampscript api. I am trying to do this within mobile connect using ampscript but I can't seem to get it to work!

Unfortunately there isn't much debugging in here so I am hoping someone with more expertise than I can assist.

Here is what I have tried:

%%[

Var @msgpass, @msgfail
SET @msgpass = "Stop Activated" 
SET @msgfail = "Stop Not Activated"

IF indexof([MSG(0)], "STOP") > 0 THEN
OutputLine(v(@msgpass))

ELSE
OutputLine(v(@msgfail))
VAR @payload, @response

SET @payload = '{
"sender": {
"name": "Phillip J Fry",
"handle": "Phillip_Fry_the_Delivery_Guy"
},
"subject": "Question",
"body": "Didn't we used to be a delivery company?",
"attachments": [],
"metadata": {}
}'

SET @request = HTTPPost("https://api2.frontapp.com/Secret:)", "application/json", @payload, @response, "Authorization", "Bearer Secret :)", "Accept-Charset", "utf-8")

ENDIF

]%%
3
  • Do you have any debugging or error handling to at least see why the data is doing? Did you validate the JSON? Bad JSON tends to cause a lot of problems... Commented Jan 4, 2019 at 21:30
  • JSON is verified. I dont have any debugging or error handling... I tried to create a cloud page to get some more debugging but get thrown a 500 error when tried to access the cloud page url (Which i assume is from the AMPSCRIPT) Commented Jan 4, 2019 at 21:32
  • that's usually how I debug, with a cloud page, or upsert the data into a DE.Honestly everything looks fine to me. I've never used HTTPPost so can't be sure if it looks ok. You might try output instead of outputline as I know that works (I'm sure outputline does too, but worth a shot). I'm also wondering if ampscript doesn't like the multi line string you are using for @payload. You might try it in a cloud page and see if it works without it. If it does, then it's just a matter of escaping those lines. Commented Jan 4, 2019 at 22:07

1 Answer 1

3

I think the issue is that you are opening the string for @payload with a single quote ' and inside of your "body" you are using a single quote in Didn't which is then closing your string at Didn'. This then makes the JSON invalid and also adds extra characters that are incorrect syntax for AMPScript.

This is a bit 'hacky', but to retain it in AMPScript (since I believe SSJS is not allowed inside of Mobile Connect), what I would do is to replace all instances of your single quotes inside your JSON with another character.

I usually use a ~ or similar char that would be very unlikely to ever be inside your content. I then do a ReplaceList() (I use this instead of Replace() because Replace() will only affect the first match of your string, where ReplaceList() will replace all matches of the selected string value.) inside of the HTTPPost, like so:

SET @payload = '{
"sender": {
"name": "Phillip J Fry",
"handle": "Phillip_Fry_the_Delivery_Guy"
},
"subject": "Question",
"body": "Didn~t we used to be a delivery company?",
"attachments": [],
"metadata": {}
}'

SET @request = HTTPPost("https://api2.frontapp.com/Secret:)", "application/json", ReplaceList(@payload, "'", "~"), @response, "Authorization", "Bearer Secret :)", "Accept-Charset", "utf-8")

This will make the object include your single quotes again in the POST, but prevent any errors due to mismatched quotes inside of your JSON.

I believe if you are pulling in the JSON from a DE field or from an outside source (instead of manually creating it in the script) and attaching it to the var, you will be fine with the single quotes inside of it, but not 100% certain on this.

You must log in to answer this question.

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