简体   繁体   English

邮递员-仅从URL获取端点

[英]Postman - Get only endpoint from a URL

Having this kind of endpoing: 具有这种结局:

https://hey.com/p1/m/p2 https://hey.com/p1/m/p2

I want to get rid of the https://hey.com and get only the /p1/m/p2 part on the Pre-request Script . 我想摆脱https://hey.com ,只获得Pre-request脚本上的/p1/m/p2部分。 I know I can do it using request.url.replace(/^.*\\/\\/[^\\/]+/, '') , getting the desired output /p1/m/p2 . 我知道我可以使用request.url.replace(/^.*\\/\\/[^\\/]+/, '') ,得到所需的输出/p1/m/p2

Is there any other way to do it without using replace or regex? 还有其他方法可以不使用replace或regex吗? Something like request.url.pathname (which is not working, obviously). request.url.pathname (显然不起作用)。

The URL above is just an example, the endpoints and urls will vary. 上面的URL仅是一个示例,端点和url会有所不同。

Bear in mind that I'm using the Pre-request Script on the Postman environment and some things may not work. 请记住,我在Postman环境中使用了Pre-request脚本 ,有些可能无法正常工作。

Thank you so much. 非常感谢。

You should be able to use the URL browser API to construct a URL object. 您应该能够使用URL浏览器API来构造URL对象。

(new URL(request.url)).pathname

If you're using the desktop version, you can use the built-in node.js API. 如果您使用的是台式机版本,则可以使用内置的node.js API。

var URL = require('url');

URL.parse(request.url).pathname

You can try something like this 您可以尝试这样的事情

var fullUrl = request.url.split('/');
fullUrl.splice(0,3);
var url = a.join('/');
console.log(url);

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

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