Hi All, I am trying to get a serialised body of a API expose endpoint. Example, I have a POST endpoint (expose) that is expecting a body :{"name": "John Smith","age": 27}But the request coming in actually contains this:{"name": "John Smith","age": 27,"bestfilmever": "break point"}Is there a way for me to capture this body (and serialize it) before it is parsed by the structure that we define for the body of the endpoint? If so, how? I would like to store the raw body coming into the endpoint.
Please refer this article on 'Customize REST API Responses'. You can handle this in On After response.
Hi Siya, not sure how this is relevant, I am not after customising the response. But even using OnbeforeRequest, not sure how I would capture the serialised body into the expose action. Care to show me a screenshot on how you would do this? thanks
There was a correction in last response (Its not 'On After response' but 'OnRequest'). I exposed a service like below where I accept a User object an input and return all the users.
Here I have added OnRequest to my exposed API which will receive the data in the input parameters ( RequestText & RequestBinary ). Here I am just logging the data.
Using Postman I tested my API and sent additional parameters than required.
and I can see that this information get logged.
In your case you can manipulate the RequestText before it is assigned back to CustomizedRequest.
Ok, that was helpful thank you, but I still needed a few steps to pass the raw JSON to my Expose action. I managed to solve it like this (would useful to hear if you would have done it differently), I was forced to replace the " to ' to avoid issues with the JSON parsing inside my endpoint logic. Step 1 - store the raw JSON in a local variable and replace any " to ' (to avoid parsing errors)Step 2 - Join my local variable to the inputStep 3 - Add a variable called rawJSON to my endpoint structure
Here is my API call:
{"name":"John Smith","email":"john.smith@email.com","rawJSON":"{\r\n 'name':'John Smith',\r\n 'email': 'john.smith@email.com',\r\n 'nickname': 'jojo',\r\n 'age': 25\r\n}"} I think if I were to store this in the DB I would replace the ' back to " and save the string so that I could then use the JSON using something like JSON Pretty Format.Thoughts on the approach?
Hi Andre, the only problem that I see with this approch is that you will have a rawJson field in the documentation, so who calls the api sees this field.