1

这是我第二次尝试更准确地解释我在寻找什么;-)

我在 Mailchimp 中设置了一个 webhook,每次出现新的观众订阅者时都会触发。Mailchimp 将 HTTP POST 请求发送到 Jira Sriptrunner REST 端点。

此请求的内容类型是application/x-www-form-urlencoded

在 Jira 端点中,我想读取请求数据。我怎样才能做到这一点?

我收到的有效载荷(原始正文)如下所示:

type=unsubscribe&fired_at=2020-05-26+07%3A04%3A42&data%5Baction%5D=unsub&data%5Breason%5D=manual&data%5Bid%5D=34f28a4516&data%5Bemail%5D=examlple%40bla.com&data%5Bemail_type%5D=html&data%5Bip_opt%5D=xx.xxx.xxx.198&data%5Bweb_id%5D=118321378&data%5Bmerges%5D%5BEMAIL%5D=example%40bla.com&data%5Bmerges%5D%5BFNAME%5D=Horst&data%5Bmerges%5D%5BLNAME%5D=Schlemmer&data%5Bmerges%5D%5BCOMPANY%5D=First&data%5Bmerges%5D%5BADDRESS%5D%5Baddr1%5D=XXX

现在我想将原始正文的数据解析为 JSON 或类似的东西。

结果可能如下所示:

{
 "web_id": 123,
 "email": "example@bla.com",
 "company": "First",
 ...
 }

与此同时,我四处搜索了一下,发现了类似 node.js“querystring”模块的东西。如果在 Groovy 中有类似的东西或任何其他方式来解析数据application/x-www-form-urlencodedjson格式化,那就太好了。

提前致以最诚挚的问候和感谢

伯恩哈德

4

3 回答 3

0
def body = "type=unsubscribe&fired_at=2020-05-26+07%3A04%3A42&data%5Baction%5D=unsub&data%5Breason%5D=manual&data%5Bid%5D=34f28a4516&data%5Bemail%5D=examlple%40bla.com&data%5Bemail_type%5D=html&data%5Bip_opt%5D=xx.xxx.xxx.198&data%5Bweb_id%5D=118321378&data%5Bmerges%5D%5BEMAIL%5D=example%40bla.com&data%5Bmerges%5D%5BFNAME%5D=Horst&data%5Bmerges%5D%5BLNAME%5D=Schlemmer&data%5Bmerges%5D%5BCOMPANY%5D=First&data%5Bmerges%5D%5BADDRESS%5D%5Baddr1%5D=XXX"

def map = body.split('&').collectEntries{e->
    e.split('=').collect{ URLDecoder.decode(it, "UTF-8") }
}

assert map.'data[merges][EMAIL]'=='example@bla.com'
map.each{println it}

印刷:

type=unsubscribe
fired_at=2020-05-26 07:04:42
data[action]=unsub
data[reason]=manual
data[id]=34f28a4516
data[email]=examlple@bla.com
data[email_type]=html
data[ip_opt]=xx.xxx.xxx.198
data[web_id]=118321378
data[merges][EMAIL]=example@bla.com
data[merges][FNAME]=Horst
data[merges][LNAME]=Schlemmer
data[merges][COMPANY]=First
data[merges][ADDRESS][addr1]=XXX
于 2020-05-27T12:46:01.900 回答
0

一个简单的不费吹灰之力的常规:

def a = '''
data[email_type]: html
data[web_id]: 123
fired_at: 2020-05-26 07:28:25
data[email]: example@bla.com
data[merges][COMPANY]: First
data[merges][FNAME]: Horst
data[ip_opt]: xx.xxx.xxx.xxx
data[merges][PHONE]: xxxxx
data[merges][ADDRESS][zip]: 33615
type: subscribe
data[list_id]: xxXXyyXX
data[merges][ADDRESS][addr1]: xxx.xxx'''

def res = [:]

a.eachLine{
  def parts = it.split( /\s*:\s*/, 2 )
  if( 2 != parts.size() ) return
  def ( k, v ) = parts
  def complexKey = ( k =~ /\[(\w+)\]/ ).findAll()
  if( complexKey ) complexKey = complexKey.last().last()
  res[ ( complexKey ?: k ).toLowerCase() ] = v
}

res

给出:

[email_type:html, web_id:123, fired_at:2020-05-26 07:28:25, 
email:example@bla.com, company:First, fname:Horst, ip_opt:xx.xxx.xxx.xxx, 
phone:xxxxx, zip:33615, type:subscribe, list_id:xxXXyyXX, addr1:xxx.xxx]
于 2020-05-26T13:12:31.800 回答
0

我终于找到了解决方案。我希望你能理解,也许它也可以帮助其他人;-)

从 daggett 的回答开始,我做了以下事情:

// Split body and remove unnecessary characters
def map = body.split('&').collectEntries{e->
    e.split('=').collect{ URLDecoder.decode(it, "UTF-8") }
}
// Processing the map to readable stuff
def prettyMap = new JsonBuilder(map).toPrettyString()
// Convert the pretty map into a json object
def slurper = new JsonSlurper()
def jsonObject = slurper.parseText(prettyMap)

(地图看起来很像 daggett 的回答。 prettyMap

然后我提取密钥:

// Finally extracting customer data
def type = jsonObject['type']

我得到了我需要的数据。例如

Type : subscribe
...
First Name : Heinz
...

感谢达格特!

于 2020-07-07T12:51:27.433 回答