对Node.js使用Python的unquote()

时间:2019-12-21 18:05:57

标签: python node.js

我正在通过Webhook接收以下格式的数据。

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

在Python中,我可以使用以下函数来转换数据。但是,我找不到使用Node.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

1 个答案:

答案 0 :(得分:0)

nodejs中的

urlib.parse.unquote等效项是unescape

nodejs中没有urlib.parse.unquote_plus等效项

但是您可以按照以下步骤自己做

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