简体   繁体   English

我无法从 wso2 中的 url 获取参数

[英]I can't get parameter from url in wso2

I know there are enough similar topics, but I can't get the parameter from the url. Help me figure it out.我知道有很多类似的主题,但我无法从 url 获取参数。帮我弄清楚。 What am I doing wrong?我究竟做错了什么? Here is an example of my code:这是我的代码示例:

<api context="/auth" name="AuthHarv" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" uri-template="/harv/token={tokenVal}">
    <inSequence>
        <log separator="&#xa;">
            <property expression="$url:uri.var.tokenVal" name="token1"/>
            <property expression="$url:uri.var.token" name="token2"/>
            <property expression="$url:tokenVal" name="token3"/>
            <property expression="$url:token" name="token4"/>
            <property expression="$url:uri.var.tokenVal" name="uri.var.token1"/>
            <property expression="$url:uri.var.token" name="uri.var.token2"/>
            <property expression="$url:tokenVal" name="uri.var.token3"/>
            <property expression="$url:token" name="uri.var.token4"/>
            <property expression="get-property('uri.var.tokenVal')" name="getToken1"/>
            <property expression="get-property('uri.var.token')" name="getToken2"/>
            <property expression="get-property('tokenVal')" name="getToken3"/>
            <property expression="get-property('token')" name="getToken4"/>
            <property expression="get-property('uri.var.tokenVal')" name="uri.var.getToken1"/>
            <property expression="get-property('uri.var.token')" name="uri.var.getToken2"/>
            <property expression="get-property('tokenVal')" name="uri.var.getToken3"/>
            <property expression="get-property('token')" name="uri.var.getToken4"/>
            <property expression="get-property('query.param.tokenVal')" name="paramToken1"/>
            <property expression="get-property('query.param.token')" name="paramToken2"/>
            <property expression="get-property('query.param.tokenVal')" name="uri.var.paramToken1"/>
            <property expression="get-property('query.param.token')" name="uri.var.paramToken2"/>
        </log>
        
        <property expression="concat('https://localhost:44343/Account/login?token=', $ctx:token)" name="uri.var.url" scope="default" type="STRING"/>
        <log separator="&#xa;">
            <property expression="$ctx:uri.var.url" name="path to harvest"/>
        </log>
        <call>
            <endpoint>
                <http method="get" uri-template="{uri.var.url}">
                    <suspendOnFailure>
                        <initialDuration>-1</initialDuration>
                        <progressionFactor>1</progressionFactor>
                    </suspendOnFailure>
                    <markForSuspension>
                        <retriesBeforeSuspension>0</retriesBeforeSuspension>
                    </markForSuspension>
                </http>
            </endpoint>
        </call>
        <log separator="&#xa;">
            <property expression="json-eval($)" name="JSON_auth_from_harvest"/>
        </log>
        <respond/>
    </inSequence>
    <outSequence/>
    <faultSequence/>
</resource>

This is what I get in the log:这是我在日志中得到的:

Direction: request方向:要求
token1 =令牌 1 =
token2 =令牌 2 =
token3 =令牌 3 =
token4 =令牌 4 =
uri.var.token1 = uri.var.token1 =
uri.var.token2 = uri.var.token2 =
uri.var.token3 = uri.var.token3 =
uri.var.token4 = uri.var.token4 =
getToken1 = token_value getToken1 = token_value
getToken2 = null getToken2 = null
getToken3 = null getToken3 = null
getToken4 = null getToken4 = null
uri.var.getToken1 = token_value uri.var.getToken1 = token_value
uri.var.getToken2 = null uri.var.getToken2 = null
uri.var.getToken3 = null uri.var.getToken3 = null
uri.var.getToken4 = null uri.var.getToken4 = null
paramToken1 = null paramToken1 = null
paramToken2 = null paramToken2 = null
uri.var.paramToken1 = null uri.var.paramToken1 = null
uri.var.paramToken2 = null uri.var.paramToken2 = null
[2022-04-13 19:34:05,164] INFO {LogMediator} - {api:AuthHarvest} To: /auth/harv/token=token_value [2022-04-13 19:34:05,164] 信息 {LogMediator} - {api:AuthHarvest} 收件人:/auth/harv/token=token_value

Why can't I get the value from $url:prop?为什么我不能从 $url:prop 中获取值? Thank you very much for your help.非常感谢您的帮助。

You have defined the uri template not entirely correctly with REST. It should be something like: uri-template="/harv?token={tokenValue}" (with question mark).您使用 REST 定义的uri template不完全正确。它应该类似于: uri-template="/harv?token={tokenValue}" (带问号)。 And then You can get the parameter value using:然后您可以使用以下方法获取参数值:

<property name="token" expression="get-property('query.param.token')"/>

or或者

<property name="tokenVal" expression="get-property('uri.var.tokenVal')"/>

And as I see, even in your template, you get the value: getToken1 = token_value and uri.var.getToken1 = token_value正如我所见,即使在您的模板中,您也可以获得值:getToken1 getToken1 = token_valueuri.var.getToken1 = token_value

The issue seems to be with how you defined your uri-template ( uri-template="/harv/token={tokenVal}" ).问题似乎与您如何定义 uri-template ( uri-template="/harv/token={tokenVal}" )有关。

Path parameter路径参数

If you want to define a path parameter, it should be.如果你想定义一个路径参数,它应该是。

uri-template="/harv/{tokenVal}"

You can access it with你可以访问它

<property name="pathParam" expression="get-property('uri.var.tokenVal')"></property>

Query parameter查询参数

If the parameter is sent as a query parameter如果参数作为查询参数发送

uri-template="/harv?token={tokenVal}"

You can access it with你可以访问它

<property name="queryParam" expression="get-property('query.param.token')"></property>
<property name="queryParam" expression="$url:token"></property>

Refer to documentation [1] to get more information on this.请参阅文档 [1] 以获得更多相关信息。

[1]-https://ei.docs.wso2.com/en/7.2.0/micro-integrator/use-cases/examples/rest_api_examples/setting-query-params-outgoing-messages/ [1]-https://ei.docs.wso2.com/en/7.2.0/micro-integrator/use-cases/examples/rest_api_examples/setting-query-params-outgoing-messages/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM