1

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
Sourcerer
  • 1,891
  • 1
  • 19
  • 32

1 Answers1

1

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.

enter image description here

Refs:

https://docs.wso2.com/display/ESB500/Script+Mediator

https://asitha.github.io/cs/programming/wso2/esb/mediators/2017/08/14/get-query-params-with-wso2-esb/

Bee
  • 12,251
  • 11
  • 46
  • 73
  • Your answer solved my problem with query params. Do you know how to get header param (or event cookie param) – Sourcerer Apr 24 '18 at 10:54
  • You need to define that header in the resource section first. Then you can access is as 'uri.var.' – Bee Apr 24 '18 at 13:03
  • Hi, it seem not work for me. I have the header in the resource, and added to CORS (if not, the gateway rejects). But the getProperty('uri.var.myHeader') returns null. I'll try again, and let you know. And thanks! – Sourcerer Apr 25 '18 at 17:45
  • I have created a code to dump all properties, and I don't see the header there var log = mc.getServiceLog(); var properties = []; var propertyKeySet = mc.getPropertyKeySet(); if (propertyKeySet !== null) { log.info('propertyKeySet is not null'); for(var item = propertyKeySet.iterator(); item.hasNext();) { var key=item.next(); log.info('Item Key='+key); var property={ key : key, value : mc.getProperty(key) }; log.info('Item Value='+property.value); properties.push(property); } } – Sourcerer Apr 25 '18 at 17:45
  • This is the headerParameter definition in swagger ` - name: headerParameter in: header required: false type: string description: Parameter in header` – Sourcerer Apr 27 '18 at 08:59
  • I have updated the original question with the full swagger. The header parameter is headerParameter. Also, you have the javascript code I'm trying. And, again, thanks! – Sourcerer Apr 27 '18 at 09:05
  • I just did and it worked. Added a screenshot to the answer. – Bee Apr 28 '18 at 06:09
  • So it some kind of configuration issue. I'll double recheck. – Sourcerer May 08 '18 at 17:09
  • In the meantime, I have tried also the mc.getPayloadJSON(), and it returns an empty object for me (is a differente operation with POST). I see other people claiming about this https://stackoverflow.com/questions/27848342/getpayloadjson-returning-an-empty-object – Sourcerer May 08 '18 at 17:10
  • Do you send the content-type header as application/json? – Bee May 09 '18 at 12:34
  • Yes, I send a header with application/json. I include my full test. -- Call curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ "campo": "prueba" }' 'https://apisandbox.example.com:8244/test/v1/inlineTest' -- Response received {"Payload":null, "Content":{}} – Sourcerer May 16 '18 at 10:56