Forum Discussion
Hi DougS. With the code that you provided, we figured out that when the webhook is sent to our endpoint, initial if condition and the second line to get the request body is working, but this statement in your code
<#assign requestBodyObj = utils.json.fromJson(requestBody) />
doesn't seem to be working and after putting the check for if the body has_content we got to know that in the POST request something is coming to our endpoint but when I'm trying to get it in the restadmin call, nothing seems to set in my metadata where I'm trying to set the request body or any value in the object which comes in the body. I read in this article https://developer.khoros.com/khoroscaredevdocs/docs/working-with-webhooks that
requestBody: The entire body of the request, read as a UTF8-encoded string
and based on this when I'm getting the request body in my variable I also tried putting ?('utf-8') but that also didn't help. Is there something that I'm missing?
Below is my endpoint code -
<#attempt>
<#assign alpha = http.request.body?trim?('utf-8') />
<#if alpha?? && alpha?has_content && alpha?is_string >
<#assign alpha = alpha?eval />
<#assign updateProfile = restadmin('/users/sso_id/id/settings/name/profile.cert_info/set?value=L1 ${alpha.id}') />
<#elseif alpha?? && alpha?has_content && alpha?is_hash >
<#assign updateProfile = restadmin('/users/sso_id/id/settings/name/profile.cert_info/set?value=L2') />
<#else>
<#assign updateProfile = restadmin('/users/sso_id/id/settings/name/profile.cert_info/set?value=L3') />
</#if>
<#recover>
<#assign updateProfile = restadmin('/users/sso_id/id/settings/name/profile.cert_info/set?value=LEVEL4') />
</#attempt>
Also for an example, in the webhook documentation from where we are calling our endpoint, something like this is what we are supposed to get in the request body -
{ "id": "628fcaa9-cfd3-4a9b-aa62-9973ac938ad6", "organization_id": "a0d325d9-d8cb-466f-b515-c767949f7e9a", "event_type": "badge.created", "occurred_at": "2014-04-01T14:41:00Z" }
In my tests, utils.json.fromJson seems to be working.
You shouldn't need to add the ?('UTF-8') to the http.request.body call, as it returns the body as a UTF-8 encoding string already.
You might want to try something like this for your endpoint:
<#function doRestV1Call uri admin=false>
<#if admin>
<#assign restCall = restadmin />
<#else>
<#assign restCall = rest />
</#if>
<#return restCall(uri) />
</#function>
<#function setProfileField ssoId name val admin=false>
<#return doRestV1Call("/users/sso_id/" + ssoId + "/settings/name/" + name + "/set?value=" + val, true) />
</#function>
<#function setCertInfo ssoId val>
<#return setProfileField(ssoId, "profile.cert_info", val?url, true) />
</#function>
<#function getCertInfo ssoId>
<#return doRestV1Call("/users/sso_id/" + ssoId + "/settings/name/profile.cert_info", true).value />
</#function>
<#assign errorMsg = "" />
<#if ("POST" == http.request.method)>
<#attempt>
<#assign alpha = http.request.body />
<#if alpha?? && alpha?has_content && alpha?is_string>
<#assign alpha = utils.json.fromJson(alpha) />
<#assign updateProfile = setCertInfo(alpha.id, "L1" + alpha.id) />
<#elseif alpha?? && alpha?has_content && alpha?is_hash>
<#-- this should not happen -->
<#assign updateProfile = setCertInfo(alpha.id, "L2") />
<#else>
<#assign errorMsg = "no request body" />
</#if>
<#recover>
<#assign errorMsg = .error />
</#attempt>
<#if errorMsg?length lt 1>
<#attempt>
<#assign profileVal = getCertInfo(alpha.id) />
<#recover>
<#assign errorMsg = .error />
</#attempt>
</#if>
<#else>
<#assign errorMsg = "request must be an HTTP POST." />
</#if>
<#if errorMsg?length gt 0>
{
"successful": false,
"message": "${errorMsg?json_string}"
}
<#else>
{
"successful": true,
"message": "profile updated",
"value": "${profileVal?json_string}"
}
</#if>
-Doug
- pp_016 years agoMentor
Hello DougS . Sorry for bothering you with my last reply. The code which you provided on Monday seems to be working. I figured it out on Friday after posting my above comment. The statement
<#assign requestBodyObj = utils.json.fromJson(requestBody) />
was not working for me initially because of God knows what reason but when I tried again it worked and I got to know why it is crucial for my requirement as it is converting JSON string to a freemarker hash. The error was in my endpoint code in line no.4
<#assign updateProfile = restadmin('/users/sso_id/id/settings/name/profile.cert_info/set?value=L1 ${alpha.id}') />
where I have given space between the text "L1" and ${alpha.id} because of which nothing was getting set in my metadata. Removing that resolved that error. Your answers and suggestions have been very helpful. Thank you.
Related Content
- 9 years ago
- 12 years ago