How to pass values (parameters) into a JSON request body for a POST API
Application Type
Service
Service Studio Version
11.50.12 (Build 49299)
Platform Version
11.13.0 (Build 31107)

I have the following OutSystems challenge. Be patient with me, I am new to OutSystems.

I wish to consume a REST API that requires POST format and the Request body is required to be in JSON format. I would like to insert parameters in the request body that I would like to change. Example Request body could be.

{

    "query": "query mySearch {\n  searchForProducts(layer: \"layer1\", source: \"Main\", first: 10, condition: {name: {queryString: \"*red*\", operator: like, ignoreCase: true}}) {\n    maxResultSizeExceeded\n    pageInfo {\n      hasPreviousPage\n      hasNextPage\n      startCursor\n      endCursor\n    }\n    pageElements {\n      id\n      name\n      approvalStatus\n      parent {\n        id\n        name\n      }\n    }\n  }\n}\n",

    "variables": null,

    "operationName": "mySearch"

  }

With a static JSON string everything is fine. An example of a parameter would be wanting to use an input parameter for the queryString and number of result in first n

So I think the challenge is how to pass input parameters into the request body.

To add a little more context I actually stored the request body in an Entity called myQueries and use GetQueriesbyID to reference the correct JSON request. I plan to store multiple query templates in the Entity and create this as a service module that is repurposed across many applications using same API.

If it is not possible to pass parameters into the request body, which would be the easiest solution, then I need to look at assembling the JSON prior to making the REST request. The JSON request body is very complex (hence the desire to reference it from an entity) so I am not sure JSON Serialize will help or is the right tool or can do this.

Any pointers or has someone come across something similar? Passing dynamic values into a POST request body.

Solution

So the solution I came up with was close to what @Daniël Kuhlmann suggested.

I broke the JSON template down into parts and used an OutSystems expression to concatenate the parts together with  the variables.

Not ideal but it worked. I am sure there is a more efficient way but for now this gets me to the next hurdle.

Hello Simon,

I am not sure if this post will help you, but request you to look into this and see if it help you.

https://www.outsystems.com/forums/discussion/70700/how-to-pass-local-variable-to-rest-api-request-body/

Thanks & Kind Regards,

Sachin

Hi Sachin,

Thanks for your thoughts. I saw this post and I don't think it applies to my use case. I am needing to pass parameters into the body of a JSON POST request.

  

mvp_badge
MVP

Hi Simon,

If you store the response text in an entity and use read it on send it on POST, can you not do a similar mechanism with the input parameters, and then do the proper text replacements of the inputs in the response?

Regards,

Daniel

Solution

So the solution I came up with was close to what @Daniël Kuhlmann suggested.

I broke the JSON template down into parts and used an OutSystems expression to concatenate the parts together with  the variables.

Not ideal but it worked. I am sure there is a more efficient way but for now this gets me to the next hurdle.

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