如何格式化步骤函数 API 调用中的标头字段

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

How do i format the headers field in step function api invoke

问题

  1. 我正在尝试通过步函数执行 AWS API。我需要在标头字段中传递 authorizationToken 值。
  2. 我收到以下错误 -
  3. 字段 'authorizationToken.$' 的值必须是包含 JSONPath STRING,但实际是一个 ARRAY (位于 /States/GetDeclarations/Parameters)
  4. 这是 API 调用的默认语法 -

"Headers": {
"Header1": [
"HeaderValue1"
],
"Header2": [
"HeaderValue2",
"HeaderValue3"
]
}

  1. 当我修改为以下内容时 -

"Headers": {
"authorizationToken": [
"1234"
],
"Header2": [
"HeaderValue2",
"HeaderValue3"
]
}

  1. 它可以正常工作。
  2. 我需要使 "authorizationToken" 的值成为一个从输入中获取其值的变量。
  3. 我的输入数据如下 -
  4. {
  5. "xxx": "123",
  6. "yyy": "123",
  7. "zzz": "123",
  8. "InputToken": "123",
  9. "aaa": "123"
  10. }
英文:

I am trying to execute a AWS api via step function. I need to pass the authorizationToken value in the header field.

  1. {
  2. "ApiEndpoint": "xyz.execute-api.ap-southeast-2.amazonaws.com",
  3. "Method": "POST",
  4. "Headers": {
  5. "authorizationToken.$": [
  6. "$.InputToken"
  7. ]
  8. },
  9. "Stage": "test",
  10. "Path": "/",
  11. "RequestBody": {
  12. "productType": [],
  13. "xxx.$": "$.xxx",
  14. "yyy.$": "$.yyy",
  15. "zzz.$": "$.zzz"
  16. },
  17. "AuthType": "IAM_ROLE"
  18. }

I am getting the following error -

The value for the field 'authorizationToken.$' must be a STRING that contains a JSONPath but was an ARRAY (at /States/GetDeclarations/Parameters)

This is the default syntax for the API invoke -

  1. "Headers": {
  2. "Header1": [
  3. "HeaderValue1"
  4. ],
  5. "Header2": [
  6. "HeaderValue2",
  7. "HeaderValue3"
  8. ]
  9. }

When i modify this to

  1. "Headers": {
  2. "authorizationToken": [
  3. "1234"
  4. ],
  5. "Header2": [
  6. "HeaderValue2",
  7. "HeaderValue3"
  8. ]
  9. }

It works fine.

I need to make the value of "authorizationToken" a variable that takes its value from the input.

My Input data looks like this

{
"xxx": "123",
"yyy": "123",
"zzz": "123",
"InputToken": "123",
"aaa": "123"
}

答案1

得分: 1

你需要使用如下所示的States.Array内置函数。这允许你将一个数组注入到Parameters块中的节点中。在这种情况下,你只想要数组中的单个项目,但你也可以包含多个项目(例如 States.Array($.item1,$.item2,$.item3))。

同时也查看其他内置函数,它们对克服诸如此类挑战非常有用。

  1. {
  2. "ApiEndpoint": "ccqk9ijm0h.execute-api.ap-southeast-2.amazonaws.com",
  3. "Method": "POST",
  4. "Headers": {
  5. "authorizationToken.$": "States.Array($.InputToken)"
  6. },
  7. "Stage": "test",
  8. "Path": "/",
  9. "RequestBody": {
  10. "productType": [],
  11. "xxx.$": "$.xxx",
  12. "yyy.$": "$.yyy",
  13. "zzz.$": "$.zzz"
  14. },
  15. "AuthType": "IAM_ROLE"
  16. }
英文:

You need to use the States.Array Intrinsic Function as I've shown below. This allows you to inject an array into a node in your Parameters block. In this case, you just want a single item in the array, but you can include multiple items as well (e.g., States.Array($.item1,$.item2,$.item3)).

Check out the other Intrinsic Functions as well, as they are handy for overcoming challenges like this.

  1. {
  2. "ApiEndpoint": "ccqk9ijm0h.execute-api.ap-southeast-2.amazonaws.com",
  3. "Method": "POST",
  4. "Headers": {
  5. "authorizationToken.$": "States.Array($.InputToken)"
  6. },
  7. "Stage": "test",
  8. "Path": "/",
  9. "RequestBody": {
  10. "productType": [],
  11. "xxx.$": "$.xxx",
  12. "yyy.$": "$.yyy",
  13. "zzz.$": "$.zzz"
  14. },
  15. "AuthType": "IAM_ROLE"
  16. }

huangapple
  • 本文由 发表于 2023年2月19日 10:03:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497560.html
匿名

发表评论

匿名网友

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

确定