Paste0() 在 API 调用中添加转义正反斜杠?

发布于 2025-01-14 11:16:34 字数 1009 浏览 4 评论 0原文

我需要使用以下格式动态调用 API:

 auth_secret <- paste0("Bearer ", secret)

  headers = c(
    `Authorization` = auth_secret,
    `Notion-Version` = '2022-02-22',
    `Content-Type` = 'application/json' )

  res <- httr::PATCH(url = paste0('https://api.notion.com/v1/pages/', id),
                     httr::add_headers(.headers = headers),
                     body = payload,
                     encode = "json")
  d <- httr::content(res)

此有效负载有效:

payload <- "{\"properties\":{\"Project\":{\"relation\":[{\"id\":\"1d148a9e-783d-47a7-b3e8-2d9c34210355\"}]}}}"

但如果我想使用 Paste0 动态创建它(因此它位于函数内部),我会在前后添加一些反斜杠:

payload <- paste0('"{\"properties\":{\"',property_name,'\":{\"relation\":[{\"id\":\"',value,'\"}]}}}"')

print(payload) 

"\"{\"properties\":{\"%7CAK%5E\":{\"relation\":[{\"id\":\"8cb9519e72ca4bbe9e0448807acb8e10\"}]}}}\""

我认为这是由于添加了一些奇怪的转义字符,但已经没有想法了。我添加了两个 \ 并遇到了同样的问题。由于 JSON 未正确传递,调用失败。有解决方法吗?

I need to dynamically make a call to an API using the following format:

 auth_secret <- paste0("Bearer ", secret)

  headers = c(
    `Authorization` = auth_secret,
    `Notion-Version` = '2022-02-22',
    `Content-Type` = 'application/json' )

  res <- httr::PATCH(url = paste0('https://api.notion.com/v1/pages/', id),
                     httr::add_headers(.headers = headers),
                     body = payload,
                     encode = "json")
  d <- httr::content(res)

This payload works:

payload <- "{\"properties\":{\"Project\":{\"relation\":[{\"id\":\"1d148a9e-783d-47a7-b3e8-2d9c34210355\"}]}}}"

But if I want to create it dynamically, using a paste0 (so it is inside of a function), I get some backslashes added before and after:

payload <- paste0('"{\"properties\":{\"',property_name,'\":{\"relation\":[{\"id\":\"',value,'\"}]}}}"')

print(payload) 

"\"{\"properties\":{\"%7CAK%5E\":{\"relation\":[{\"id\":\"8cb9519e72ca4bbe9e0448807acb8e10\"}]}}}\""

I presume this is due to some weird escape character being added but have run out of ideas. I've added two \ and have gotten same issue. Call fails as JSON is not passed correctly. Is there a workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双马尾 2025-01-21 11:16:34

这显然与 paste0 正在转义甚至您不想转义的双引号有关。

毫无疑问,有人知道细节。然而,当我用 paste0 得到奇怪的东西时,我只使用 sprintf,这似乎在这种情况下有效:

property_name = "Projects"
value = "1d148a9e-783d-47a7-b3e8-2d9c34210355"
payload  <- sprintf(
    "{\"properties\":{\"%s\":{\"relation\":[{\"id\":\"%s\"}]}}}",
    property_name, value
)
print(payload)
# [1] "{\"properties\":{\"Projects\":{\"relation\":[{\"id\":\"1d148a9e-783d-47a7-b3e8-2d9c34210355\"}]}}}"

This is obviously something to do with the fact that paste0 is escaping even the double quotes you do not want to escape.

No doubt someone is aware of the details. However, when I get weird stuff with paste0, I just use sprintf, which seems to work in this case:

property_name = "Projects"
value = "1d148a9e-783d-47a7-b3e8-2d9c34210355"
payload  <- sprintf(
    "{\"properties\":{\"%s\":{\"relation\":[{\"id\":\"%s\"}]}}}",
    property_name, value
)
print(payload)
# [1] "{\"properties\":{\"Projects\":{\"relation\":[{\"id\":\"1d148a9e-783d-47a7-b3e8-2d9c34210355\"}]}}}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文