admin管理员组文章数量:1315941
I am using json server and axios
result from header
link: "<http://localhost:3001/posts?_page=1>; rel="first", <http://localhost:3001/posts?_page=2>; rel="next", <http://localhost:3001/posts?_page=5>; rel="last""
How can I use/access these data from link? There seems to be no information about how to parse or access this aside from github. I tried link.rels[:last]
like from github but it doesnt work.
I am using json server and axios
result from header
link: "<http://localhost:3001/posts?_page=1>; rel="first", <http://localhost:3001/posts?_page=2>; rel="next", <http://localhost:3001/posts?_page=5>; rel="last""
How can I use/access these data from link? There seems to be no information about how to parse or access this aside from github. I tried link.rels[:last]
like from github but it doesnt work.
- 2 Did you check this link: stackoverflow./questions/8735792/…? – 31piy Commented Apr 15, 2018 at 3:48
- I had not seen that one. very helpful – Catfish Commented Apr 15, 2018 at 17:51
- @Catfish, any update on this? – Tarun Lalwani Commented Apr 20, 2018 at 8:51
- @TarunLalwani The link from 31piy is very helpful. It has a library to simply do this. I also decided to use the octokit js lib which has methods for handling this github./octokit/rest.js – Catfish Commented Apr 20, 2018 at 19:56
2 Answers
Reset to default 7 +25Since JS is quite flexible you can simply use
data = 'link: "<http://localhost:3001/posts?_page=1>; rel="first", <http://localhost:3001/posts?_page=2>; rel="next", <http://localhost:3001/posts?_page=5>; rel="last""'
function parseData(data) {
let arrData = data.split("link:")
data = arrData.length == 2? arrData[1]: data;
let parsed_data = {}
arrData = data.split(",")
for (d of arrData){
linkInfo = /<([^>]+)>;\s+rel="([^"]+)"/ig.exec(d)
parsed_data[linkInfo[2]]=linkInfo[1]
}
return parsed_data;
}
console.log(parseData(data))
The output is
{ first: 'http://localhost:3001/posts?_page=1',
next: 'http://localhost:3001/posts?_page=2',
last: 'http://localhost:3001/posts?_page=5' }
var data = 'link: "<http://localhost:3001/posts?_page=1>; rel="first", <http://localhost:3001/posts?_page=2>; rel="next", <http://localhost:3001/posts?_page=5>; rel="last""'
var linkRegex = /\<([^>]+)/g;
var relRegex = /rel="([^"]+)/g;
var linkArray = [];
var relArray = [];
var finalResult = {};
var temp;
while ((temp = linkRegex.exec(data)) != null) {
linkArray.push(temp[1]);
}
while ((temp = relRegex.exec(data)) != null) {
relArray.push(temp[1]);
}
finalResult = relArray.reduce((object, value, index) => {
object[value] = linkArray[index];
return object;
}, {});
console.log(finalResult);
本文标签: javascriptHow to access 39rel39 from Links in header Hypermedia link relationsStack Overflow
版权声明:本文标题:javascript - How to access 'rel' from Links in header? Hypermedia link relations - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741995926a2410042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论