39
Views
5
Comments
Is it possible to get a serialized body of a rest api without defining a structure
Question
Application Type
Service

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. 

2019-01-07 16-04-16
Siya
 
MVP

Please refer this article on 'Customize REST API Responses'. You can handle this in On After response.

2018-08-04 18-19-00
André Pinho

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

2019-01-07 16-04-16
Siya
 
MVP

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. 

2018-08-04 18-19-00
André Pinho

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 input
Step 3 - Add a variable called rawJSON to my endpoint structure


Here is my API call:


Here is the logmessage inside the endpoint:

{"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?

2024-02-07 07-35-05
Alessandro Bognetti

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.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.