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

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

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

问题

我正在尝试通过步函数执行 AWS API。我需要在标头字段中传递 authorizationToken 值。

我收到以下错误 -

字段 'authorizationToken.$' 的值必须是包含 JSONPath 的 STRING,但实际是一个 ARRAY (位于 /States/GetDeclarations/Parameters)

这是 API 调用的默认语法 -

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


当我修改为以下内容时 -

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


它可以正常工作。

我需要使 "authorizationToken" 的值成为一个从输入中获取其值的变量。

我的输入数据如下 -

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

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

{
  "ApiEndpoint": "xyz.execute-api.ap-southeast-2.amazonaws.com",
  "Method": "POST",
  "Headers": {
    "authorizationToken.$": [
      "$.InputToken"
    ]
  },
  "Stage": "test",
  "Path": "/",
  "RequestBody": {
    "productType": [],
    "xxx.$": "$.xxx",
    "yyy.$": "$.yyy",
    "zzz.$": "$.zzz"
  },
  "AuthType": "IAM_ROLE"
}

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 -

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

When i modify this to

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

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))。

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

{
  "ApiEndpoint": "ccqk9ijm0h.execute-api.ap-southeast-2.amazonaws.com",
  "Method": "POST",
  "Headers": {
    "authorizationToken.$": "States.Array($.InputToken)"
  },
  "Stage": "test",
  "Path": "/",
  "RequestBody": {
    "productType": [],
    "xxx.$": "$.xxx",
    "yyy.$": "$.yyy",
    "zzz.$": "$.zzz"
  },
  "AuthType": "IAM_ROLE"
}
英文:

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.

{
  "ApiEndpoint": "ccqk9ijm0h.execute-api.ap-southeast-2.amazonaws.com",
  "Method": "POST",
  "Headers": {
    "authorizationToken.$": "States.Array($.InputToken)"
  },
  "Stage": "test",
  "Path": "/",
  "RequestBody": {
    "productType": [],
    "xxx.$": "$.xxx",
    "yyy.$": "$.yyy",
    "zzz.$": "$.zzz"
  },
  "AuthType": "IAM_ROLE"
}

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:

确定