WSO2 内联原型制作:如何获得与 "path type" 不同的变量?
WSO2 inline prototyping: How to get variables different that "path type"?
我正在使用 WSO2 2.1.0 内联原型制作功能,如中所示
Create a Prototyped API with an Inline Script
此示例使用 Synapse ScriptMessageContext 来获取变量。
但是如果这是一个 "path" 变量,我只能得到变量值。
如何获取其他类型(查询、正文)变量?
注意:我的代码与示例相同,但我已将 API 中的变量定义为查询
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
您可以使用 mc.getPayloadJSON()
获取 JSON 负载,并使用 mc.get-property('query.param.arg1')
读取查询参数。
你可以这样阅读headers。
参考文献:
我正在使用 WSO2 2.1.0 内联原型制作功能,如中所示 Create a Prototyped API with an Inline Script
此示例使用 Synapse ScriptMessageContext 来获取变量。 但是如果这是一个 "path" 变量,我只能得到变量值。
如何获取其他类型(查询、正文)变量?
注意:我的代码与示例相同,但我已将 API 中的变量定义为查询
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
您可以使用 mc.getPayloadJSON()
获取 JSON 负载,并使用 mc.get-property('query.param.arg1')
读取查询参数。
你可以这样阅读headers。
参考文献: