HTTP 请求中的 Bearer Token

huangapple go评论67阅读模式
英文:

Bearer Token on HTTP Request

问题

我尝试用一个变量来替换令牌,这样我就不需要硬编码令牌了,但似乎我无法这样做。HTTP 请求中的 Bearer Token
这是来自Postman的输出
`"您调用了函数'+',并使用以下参数:
1: String ("Bearer ")
2: String ("eyJhbGciOiJSUzI1NiIsImprdSI6Imh0dHBzOi8vcHQtZGVtby1jZi1ldTEwLXNieC5hdXRoZW50...")

但它期望其中一个组合:
(Array, Any)
(Date, Period)
(DateTime, Period)
(LocalDateTime, Period)
(LocalTime, Period)
(Number, Number)
(Period, DateTime)
(Period, LocalDateTime)
(Period, Time)
(Period, Date)
(Period, LocalTime)
(Time, Period)

4| "Authorization" : 'Bearer ' + payload.message,
^^^^^^^^^^^^^^^^^^^^^^^^^^^
跟踪:
在匿名::main (line: 4, column: 19) 中评估表达式:"output application/java

{
"Authorization" : 'Bearer ' + payload.message,
"x-qos" : "1"
}"。
`

我期望它会正常工作,因为如果我硬编码令牌,流程将正常工作。

英文:

I try to replace the token with a variable so that i dont need to hardcode the token but it seems I cant do it.HTTP 请求中的 Bearer Token
and this is the output from postman
`"You called the function '+' with these arguments:
1: String ("Bearer ")
2: String ("eyJhbGciOiJSUzI1NiIsImprdSI6Imh0dHBzOi8vcHQtZGVtby1jZi1ldTEwLXNieC5hdXRoZW50...)

But it expects one of these combinations:
(Array, Any)
(Date, Period)
(DateTime, Period)
(LocalDateTime, Period)
(LocalTime, Period)
(Number, Number)
(Period, DateTime)
(Period, LocalDateTime)
(Period, Time)
(Period, Date)
(Period, LocalTime)
(Time, Period)

4| "Authorization" : 'Bearer ' + payload.message,
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Trace:
at anonymous::main (line: 4, column: 19)" evaluating expression: "output application/java

{
"Authorization" : 'Bearer ' + payload.message,
"x-qos" : "1"
}".
`

I expect it will wokr just fines since if i hardcode the token. The flow will work just fine.

答案1

得分: 1

以下是您要翻译的内容:

希望您对MuleSoft和DataWeave是新手。正如错误消息所指示的那样,您不能使用“+”运算符进行字符串连接。要连接两个或多个字符串,一种选择是使用++函数。另一种更高级的技巧是使用$()语法。您可以在这里了解更多关于这些方法的信息。

示例:

使用**++**进行连接

%dw 2.0
output application/json
---
{
    "Authorization": "Bearer " ++ payload.message
}

使用**$()**进行连接

%dw 2.0
output application/json
---
{
    "Authorization": "Bearer $(payload.message)"
}
英文:

Hope you are new to MuleSoft and DataWeave. As the error message indicates, you cannot use the "+" operator for string concatenation. To concatenate two or more strings, one option is to use the ++ function. Another more advanced technique is to use the $() syntax. You can read more about these methods here

Example:

Using ++ for concatenation

%dw 2.0
output application/json
---
{
    "Authorization": "Bearer " ++ payload.message
}

Using $() for concatenation

%dw 2.0
output application/json
---
{
    "Authorization": "Bearer $(payload.message)"
}

huangapple
  • 本文由 发表于 2023年6月13日 14:11:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定