PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
SendGrid: Save and verify a topic opt-out
See more SendGrid Examples
Use this when a signed-in reader turns off Product updates. Look up the configured group before submitting: SendGrid warns that a nonexistent or deleted group can cause a global suppression instead. Keep the group active while processing preferences. Deleting a marketing contact is not a substitute for recording an opt-out.
Add group suppressions ยท Check specific addresses
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Resp
string ls_Address
oleobject loo_GroupInfo
oleobject loo_Addresses
oleobject loo_Matches
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Http.RequireSslCertVerify = 1
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
loo_Http.AuthToken = "SENDGRID_API_KEY"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
// Take the address from the authenticated account and the group ID from configuration.
ls_Address = "reader@example.com"
loo_Http.SetUrlVar("group","12345")
li_Success = loo_Http.HttpNoBody("GET","https://api.sendgrid.com/v3/asm/groups/{$group}",loo_Resp)
if li_Success <> 1 then
if loo_Resp.StatusCode = 0 then
Write-Debug loo_Http.LastErrorText
else
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
end if
destroy loo_Http
destroy loo_Resp
return
end if
if loo_Resp.StatusCode <> 200 then
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
destroy loo_Http
destroy loo_Resp
return
end if
loo_GroupInfo = create oleobject
li_rc = loo_GroupInfo.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_GroupInfo.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Unexpected response: expected JSON."
destroy loo_Http
destroy loo_Resp
destroy loo_GroupInfo
return
end if
if loo_GroupInfo.StringOfEquals("name","Product updates",1) <> 1 then
Write-Debug "Unexpected unsubscribe group; no preference was changed."
destroy loo_Http
destroy loo_Resp
destroy loo_GroupInfo
return
end if
loo_Addresses = create oleobject
li_rc = loo_Addresses.ConnectToNewObject("Chilkat.JsonObject")
loo_Addresses.UpdateString("recipient_emails[0]",ls_Address)
li_Success = loo_Http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions",loo_Addresses,"application/json",loo_Resp)
if li_Success <> 1 then
if loo_Resp.StatusCode = 0 then
Write-Debug loo_Http.LastErrorText
else
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
end if
destroy loo_Http
destroy loo_Resp
destroy loo_GroupInfo
destroy loo_Addresses
return
end if
if loo_Resp.StatusCode <> 201 then
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
destroy loo_Http
destroy loo_Resp
destroy loo_GroupInfo
destroy loo_Addresses
return
end if
li_Success = loo_Http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search",loo_Addresses,"application/json",loo_Resp)
if li_Success <> 1 then
if loo_Resp.StatusCode = 0 then
Write-Debug loo_Http.LastErrorText
else
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
end if
destroy loo_Http
destroy loo_Resp
destroy loo_GroupInfo
destroy loo_Addresses
return
end if
if loo_Resp.StatusCode <> 200 then
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
destroy loo_Http
destroy loo_Resp
destroy loo_GroupInfo
destroy loo_Addresses
return
end if
loo_Matches = create oleobject
li_rc = loo_Matches.ConnectToNewObject("Chilkat.JsonArray")
li_Success = loo_Matches.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Unexpected response: expected JSON."
destroy loo_Http
destroy loo_Resp
destroy loo_GroupInfo
destroy loo_Addresses
destroy loo_Matches
return
end if
if loo_Matches.Size = 1 then
Write-Debug "Confirmed opt-out: " + loo_Matches.StringAt(0)
else
Write-Debug "Opt-out not confirmed. Recheck this group."
end if
destroy loo_Http
destroy loo_Resp
destroy loo_GroupInfo
destroy loo_Addresses
destroy loo_Matches