Seb*_*Seb 6 javascript escaping node.js linkedin-api axios
我正在尝试通过他们的 API 在 LinkedIn 上发帖。端点https://api.linkedin.com/rest/posts。
在我尝试发布带有角色的内容之前,集成工作正常(。重新编辑的示例:
{
author: '',
commentary: 'a line\n' +
'\n' +
'another line...\n' +
'\n' +
'\n' +
'This gets posted (it will get cut off from here)\n' +
'another line\n'
...
}
Run Code Online (Sandbox Code Playgroud)
如果尝试对字符串进行 url 编码以及 JSON.stringify 整个对象。第一个发布的所有转义字符仍然存在(即:)a%20line%20,并且也会被切断,因为encodeURI不会转义()。后者会在 API 中抛出一个错误,提示“注释字符串格式错误”之类的内容。
有什么想法如何解决这个问题吗?
编辑:
但是:如果我这样做,\\(API 将返回"The input string format is invalid"
好吧,问题确实是一些奇怪的逃逸东西。
text.replace(/[\(*\)\[\]\{\}<>@|~_]/gm, (x) => "\\" + x)通过在字符串上运行来解决。
我也遇到了同样的问题,以下是我在 PHP 中修复它的方法:
$sanitizedDescription = preg_replace_callback('/([\(\)\{\}\[\]])|([@*<>\\\\\_~])/m', function ($matches) {
return '\\'.$matches[0];
}, $descriptionToSanitize);
Run Code Online (Sandbox Code Playgroud)
上面的代码片段的作用是匹配正则表达式中提到的每个特殊字符,并将它们替换为转义字符,以便 LinkedIn 将它们视为文字。
| 归档时间: |
|
| 查看次数: |
1988 次 |
| 最近记录: |