I'm using the WSO2 2.1.0 inline prototyping feature, as shown in Create a Prototyped API with an Inline Script
This sample use the Synapse ScriptMessageContext to get the variables. But I'm only able to get the variable value if this is a "path" variable.
How can I get the other types (query, body) variables?
NOTE: My code is the same that example, but I have defined the variable in the API as query
swagger: '2.0'
paths:
'/inlineTest/{pathParameter}':
get:
responses:
'200':
description: ''
parameters:
- name: pathParameter
in: path
required: true
type: string
description: Parameter in path
- name: queryArrayParameter
in: query
required: false
type: array
items:
type: string
uniqueItems: true
description: >-
Parameter to test comma separated arrays in URL/Query. Items has to
be unique
- name: queryParameter
in: query
required: true
type: string
description: Parameter in the query
- name: headerParameter
in: header
required: false
type: string
description: Parameter in header
- name: responseCode
in: query
required: false
type: integer
format: int32
minimum: 100
maximum: 599
default: 200
description: Expected response code (see inline js)
produces:
- application/json
summary: Test of synapse mediator script
description: >-
This is a test for checking inline prototype capabilities (using
javascript)
x-mediation-script: "var log = mc.getServiceLog();\nvar properties = [];\nvar propertyKeySet = mc.getPropertyKeySet();\nif (propertyKeySet !== null) {\n log.info('propertyKeySet is not null');\n\tfor(var item = propertyKeySet.iterator(); item.hasNext();) {\n var key=item.next();\n log.info('Item Key='+key);\n var property={\n key : key,\n value : mc.getProperty(key)\n };\n log.info('Item Value='+property.value);\n properties.push(property);\n }\n}\nvar pathParameter = mc.getProperty('uri.var.pathParameter');\nvar queryParameter = mc.getProperty('query.param.queryParameter');\nvar queryArrayParameter = mc.getProperty('query.param.queryArrayParameter');\n// IMPORTANT: To use header parameters, you have to enable CORS configuration and declare the header there\nvar headerParameter = mc.getProperty('uri.var.headerParameter');\nvar formParameter = mc.getProperty('uri.var.formParameter');\nvar responseCode = mc.getProperty('query.param.responseCode');\nvar response = {\n\tpathParameter : pathParameter,\n\tqueryParameter : queryParameter,\n queryArrayParameter : queryArrayParameter,\n\theaderParameter : headerParameter,\n formParameter : 'PENDING: Not implemented yet',\n\tproperties : properties\n};\n// Set the response type\nmc.setProperty('CONTENT_TYPE', 'application/json');\n// Set the response code\nmc.setProperty('HTTP_SC', responseCode);\nmc.setPayloadJSON(response);"
x-auth-type: Application & Application User
x-throttling-tier: Unlimited
info:
title: Test
version: v1
description: Test API - In ITDEV
You can use mc.getPayloadJSON()
to get the JSON payload, and mc.get-property('query.param.arg1')
to read query params.
You can read headers like this.
Refs: