I have a request, where I'm sending some values fetched from the response of the previous API calls, as strings within an array.
{"content_id":[${identifier_trending_INDIVIDUAL},${identifier_fetch_INDIVIDUAL},${identifier_interestBased_INDIVIDUAL}]}
If one of the variables doesn't have any value for e.g.: identifier_fetch_INDIVIDUAL is an empty string, I don't want to pass it. It should be included in the body of the request only when it has a value. Is it possible to achieve this using any preprocessor element?
You can achieve this using JSR223 PreProcessor and the following Groovy code:
def payload = [:]
def content = []
content += vars.get('identifier_trending_INDIVIDUAL') ?: []
content += vars.get('identifier_fetch_INDIVIDUAL') ?: []
content += vars.get('identifier_interestBased_INDIVIDUAL') ?: []
payload.put('content_id', content)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
You can reference generated value in the HTTP Request sampler's body as ${payload}
More information: