Forum Discussion

ronaksomani's avatar
9 years ago

http.request to retrieve JSON Object in endpoint

Hello,

 

I created a new endpoint, and calling it from a component using Ajax POST method.

post request payload is json object as below  : 

{

  "key1":"value1",

  "key2": "value2"

}

 

In endpoint I am trying to retrieve this payload, but not able to do so. 

Tried to make use of http.request methods, but they are helpful only if I have a key for this complete json payload.

 

http.request.attributes.name.get("attribute_name", default_value)

 

Here, I do not have "attribute_name" to be passed, as I need complete object.

 

Am I missing something here, or can some one help with approach to do so ?

 

Thanks in Advance !

  • DougS's avatar
    DougS
    Khoros Oracle

    Hi ronaksomani,

     

    Can you add the JSON payload as a request parameter? If you do that, then you can use the http.request.parameters object to get the parameter from the request. If your parameter name is "jsonpayload", you would use the following:

     

    <#assing jsonStr = http.request.parameters.name.get("jsonpayload", "") />

    If you are just passing the JSON along to another API call (using http.client), I think it makes sense to use this approach. If however you are using the JSON string to store parameters to pass to your endpoint, you might want to consider just passing the parameters themselves instead of using a JSON object. Converting a JSON string to something usable in your endpoint might be tricky, because you need to make sure you do so in a way that does not open your endpoint up to being used for script injection.

     

    -Doug

    • ronaksomani's avatar
      ronaksomani
      Mentor

      Hi DougS,

       

      Thanks for response.

      I cannot add JSON payload as request parameter, as I do not have control over that code. Thus not able to retrieve it with parameter name. 

       

      I was looking for approach, where I can retrieve complete Request body, and pass it on to request body for http.client call.

      • DougS's avatar
        DougS
        Khoros Oracle

        Just to make sure understand this, you do not have control over the client-side logic that is going to be submitting the request to the endpoint you are creating, and whoever controls that code will not be able to modify it to send the JSON as a request parameter instead of in the request body (payload), correct?

         

        Unfortunately we do not currently have a way to parse a request payload from the request via Freemarker. While we do have methods in the http.request object to get request parameters and request headers, we do not currently provide a way to get the entire request body. You could work with our Professional Services group - they are able to add a java-based endpoint that could do this until we have provided a way to do this via Freemarker.