简体   繁体   English

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

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

I am using Google Apps Script to make a series of HTTP Requests.我正在使用 Google Apps Script 发出一系列 HTTP 请求。 The endpoint I have been using just switched to cursor-based pagination.我一直在使用的端点刚刚切换到基于游标的分页。

The response looks like this.响应看起来像这样。

{...
 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"
}

I can use response['Link'] to get it down to我可以使用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"

Is there a way to extract page_info reliably from the "next" URL without regular expression?有没有一种方法可以在没有正则表达式的情况"next""next" URL 中可靠地提取page_info I am fine resorting to regular expression but I wondered if there was specific method for getting it.我很好地诉诸正则表达式,但我想知道是否有特定的方法来获取它。

Thanks in advance for your help.在此先感谢您的帮助。 I dabble and get that I still have a ton to learn.我涉足并发现我还有很多东西要学。

You can use a regex to extract the URL and whether the link is the next or previous page.您可以使用正则表达式来提取 URL 以及链接是下一页还是上一页。

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

To use this against your code you you could do something like this:要对您的代码使用它,您可以执行以下操作:

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