Forum Discussion
Hi luk ,
Unfortunately we have not added a way to do this yet, but I have taken to heart how long it has been and have added a pull request to add in support for parsing the request body (named http.request.body, in endpoints only) and a utils object for converting to/from a JSON string to a Freemarker hash (utils.json.toJson(hash) and utils.json.fromJson(string)).
I'll update this post once I know what version of the Community product it will go into.
Thanks,
-Doug
Hey DougS ,
Is there any update regarding what you have mentioned in your comment because I couldn't find anything related to "get request body in the endpoint" in the current 19.4 and 19.5 release notes. I am also stuck with the same issue mentioned above about how to get payload of a Webhook's POST request in an endpoint. If you can provide any update on this then it will be really helpful.
Thanks in advance.
- DougS6 years agoKhoros Oracle
We actually added this back in 19.2, but looks like it didn't make it into any release notes or into our Freemarker documentation -- I will make sure this gets added to our Freemarker documentation. In the meantime, you should be able to do this to parse JSON out of the request body:
<#assign requestBody = http.request.body /> <#assign requestBodyObj = utils.json.fromJson(requestBody) />
(Please note that the http.request.body object will only work when used in Endpoints -- it will throw an exception if used in a component on a community page)
-Doug
- pp_016 years agoMentor
Hey DougS . Thanks for replying. I tried what you have mentioned above and that doesn't seem to be working when I am sending a webhook to my endpoint where I'm trying to get the request body. I basically just wrote your above-mentioned code and then I'm taking the requestBodyObj and putting it in a restadmin call as a variable to update a metadata field but nothing is getting set over there. Is there a different way to do that with your given code?
- DougS6 years agoKhoros Oracle
I tested this out on a site that was on a version that is higher than 19.1:
First I checked what version my site was on by going to https://mycommunitystagesite.com/status/version (replaced mycommunitystagesite with my community stage site's domain name) -- the site I was using was on 19.2 (19.2-release r1905141529).
After I confirmed my site was on 19.2, I added an endpoint to test out this functionality with. I went to Studio > Endpoints tab and created an endpoint named "test-request-body-json" with content type set to "application/json" and this Freemarker logic:
<#if ("POST" == http.request.method)> <#assign requestBody = http.request.body?trim /> <#if requestBody?starts_with("{")> <#assign requestBodyObj = utils.json.fromJson(requestBody) /> { "testValue": "${requestBodyObj.test}" } </#if> </#if>
I then issued an HTTP POST to the endpoint with a JSON payload that it expects to deal with (replaced the URL with the correct one for the endpoint I just created):
curl -X POST \ https://mycommunitystagesite.com/plugins/custom/mycompany/mycommunity/test-request-body-json \ -H 'Content-Type: application/json' \ -d '{ "test": "test value" }'
I got back the following response when I made that call:
HTTP/1.1 200 status: 200 Date: Mon, 08 Jul 2019 18:17:56 GMT Server: Apache Set-Cookie: LithiumVisitor=~27qCFEIuWNM27x0WA~KF9NHbFXpA8CcsDu2w8X0PiU5WUHrjc0zUqkczJSAbEV9sa_mN8fhAvAyycO7nbT846YrhqNV-6Sv_4SJi-aUw..; Expires=Thu, 05-Jul-2029 18:17:56 GMT; Path=/; HttpOnly Pragma: no-cache Expires: Thu, 01 Jan 1970 00:00:00 GMT Cache-Control: no-cache Cache-Control: no-store Vary: Origin,Access-Control-Request-Headers,Access-Control-Request-Method,Access-Control-Allow-Credentials,Access-Control-Max-Age,Accept-Encoding Content-Language: en-US Content-Encoding: gzip Keep-Alive: timeout=10, max=150 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: application/json;charset=UTF-8 { "testValue": "test value" }
I hope that helps you get things sorted. If you find you are on a version that is lower than 19.2, you can open a support case to request an upgrade to the stage site.
-Doug
Related Content
- 9 years ago
- 12 years ago