如何替换 NodeJS URL 中的路径参数

Poc*_*Poc 3 javascript url node.js

问题陈述:在基于 NodeJS(Javascript) 的 API 自动化框架中调用 API 之前替换 URL 中的路径参数。

给定网址: https://api.spotify.com/v1/albums/{id}

{id}需要用值替换Sunshine

预期网址: https://api.spotify.com/v1/albums/Sunshine

我在 StackOverflow 中看到了一些问题。但它们更多的是与替换价值相关query_params,而不是替换path_params

我正在尝试使用这种方法,但此代码不起作用。

var href = new URL('https://api.spotify.com/v1/albums/{id}');
href.searchParams.set('{id}', 'Sunshine');
console.log(href.toString());
Run Code Online (Sandbox Code Playgroud)

任何让我知道如何做到这一点的帮助都会有很大帮助。

FZs*_*FZs 5

这无法通过 API 完成URL,您必须进行字符串替换:

var href = 'https://api.spotify.com/v1/albums/{id}'.replace('{id}', 'Sunshine');
console.log(href);
Run Code Online (Sandbox Code Playgroud)