繁体   English   中英

从 HTTP 请求响应标头中提取基于光标的分页 URL - Google Apps 脚本

[英]Extract From Cursor-Based Pagination URL from HTTP Request Response Headers - Google Apps Script

我正在使用 Google Apps Script 发出一系列 HTTP 请求。 我一直在使用的端点刚刚切换到基于游标的分页。

响应看起来像这样。

{...
 Link=
  <https://x.shopify.com/admin/api/2019-10/inventory_levels.json?limit=250&page_info=abc>;rel="previous",
  <https://x.shopify.com/admin/api/2019-10/inventory_levels.json?limit=250&page_info=def>;rel="next"
}

我可以使用response['Link']将其归结为

<https://x.shopify.com/admin/api/2019-10/inventory_levels.json?limit=250&page_info=abc>;rel="previous",
<https://x.shopify.com/admin/api/2019-10/inventory_levels.json?limit=250&page_info=def>;rel="next"

有没有一种方法可以在没有正则表达式的情况"next""next" URL 中可靠地提取page_info 我很好地诉诸正则表达式,但我想知道是否有特定的方法来获取它。

在此先感谢您的帮助。 我涉足并发现我还有很多东西要学。

您可以使用正则表达式来提取 URL 以及链接是下一页还是上一页。

/<(.*)>; rel=\"(.*)\"/

要对您的代码使用它,您可以执行以下操作:

const urls = headers.links.map(link => {
  const linkContents = link.match(/<(.*)>; rel=\"(.*)\"/)

  const url = linkContents[1]
  const type = linkContents[2] // next or previous

  return { url, type }
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM