如何从adf webhook活动将输入参数传递到azure python3 runbook

2024-06-09 17:41:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将参数从azure data factory pipeline webhook活动传递到我的azure automation python runbook。 我该怎么做


Tags: data参数pipelinefactorywebhookazureautomationrunbook
1条回答
网友
1楼 · 发布于 2024-06-09 17:41:16

对于这个问题,我在我这边进行了测试,发现很难通过数据工厂管道中的webhook活动请求python runbook。runbook中我的python代码如下所示:

#!/usr/bin/env python3

import sys
import re
import json
from urllib import parse

def fixJSON(string):
  res = re.sub(r"([^{}:,]+):([^{}:,]+),?", r'"\1":"\2",', string )
  res = re.sub('"[ ]*,[ ]*}', '"}', res)
  # and empty string
  res = re.sub("\"(''|\")\"", '""', res)
  # property : {...}
  res = re.sub(r"([^{}:,]+)(?=:{)", r'"\1"', res )
  return res

print("===")

test = str(sys.argv[1])
jsonStr = fixJSON(test)

jsonStr = jsonStr.replace("\"\"", "\"")
print(jsonStr)

jsonData = json.loads(jsonStr)
print(jsonData["RequestBody"])

我用请求体测试它:

{
    "name":"hury"
}

如果我在postman中测试它,如下面的屏幕截图(请求正文中没有换行符),它工作正常:

enter image description here

但是,如果请求体中有换行符(如下面的屏幕截图),则在python runbook中执行\r\n时总是失败。不幸的是,我们似乎无法删除python runbook中的换行符(我使用许多方法进行测试,例如replace("\r\n", "")xxx.rstrip(),所有这些方法都不起作用) enter image description here

如果我们只需要在postman中请求,我们可以设置请求正文,而不使用换行符。但是,如果我们在data factory的webhook活动中请求它,则请求正文似乎将使用换行符进行格式化,尽管我只是将请求正文设置为一行: enter image description here

所以我建议您使用“Web”活动而不是“Webhook”活动,因为在“Web”活动中,我们可以输入字符串而不是json数据作为请求体。若在“Webhook”活动中,它不允许我们在请求体中输入无效的json数据

enter image description here

相关问题 更多 >