将Python的unquote()用于节点.js

2024-04-29 00:06:44 发布

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

我通过webhook接收以下格式的数据。你知道吗

{
  "Body": "Test+131415+5%2B5%3D10",
  "To": "whatsapp%3A%2B4915735992273",
  "From": "whatsapp%3A%2B491603817902",
}

在Python中,我可以使用以下函数来转换数据。然而,我找不到一个方法节点.js得到同样的结果。你知道吗

def get_from(from: str) -> int:
    """
    Replace %xx escapes by their single-character equivalent.
    Only return the part that is behind the plus sign.
    """
    receiver = unquote(receiver).split("+")
    return receiver[1]


def get_body(body: str) -> str:
    """
    Replace %xx escapes by their single-character equivalent.
    _plus additionally replaces plus signs by spaces.
    """
    body = unquote_plus(body)
    return body

Tags: 数据fromgetbyreturndefbodyplus
1条回答
网友
1楼 · 发布于 2024-04-29 00:06:44

你知道吗urlib.parse.unquote文件nodejs中的等价项是unescape

没有等效的urlib.parse.unquote\u加号在nodejs中

但你可以自己做如下

const { unescape } = require('querystring');

const data = {
  "Body": "Test+131415+5%2B5%3D10",
  "To": "whatsapp%3A%2B4915735992273",
  "From": "whatsapp%3A%2B491603817902",
};

function getFrom(from) {
  return unescape(from).split('+')[1];
}

function getBody(body) {
  return unescape(body.replace(/\+/g, ' '));
}

console.log(getFrom(data.From));
console.log(getBody(data.Body));

相关问题 更多 >