Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace path parameter in URL NodeJS

Problem Statement: To replace path parameter in URL before making a call to API in NodeJS(Javascript) based API automation framework.

Given URL: https://api.spotify.com/v1/albums/{id}

{id} needs to be replaced with value Sunshine

Expected URL: https://api.spotify.com/v1/albums/Sunshine

I have seen certain questions in StackOverflow. But they are more related to replace query_params value but not to replace path_params.

I am trying to do with this approach, but this code does not work.

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

Any help to let me know how to do this would be of great help.

like image 916
Poc Avatar asked Apr 26 '26 08:04

Poc


1 Answers

This cannot be done with the URL API, you'll have to do a string replacement instead:

var href = 'https://api.spotify.com/v1/albums/{id}'.replace('{id}', 'Sunshine');
console.log(href);
like image 94
FZs Avatar answered Apr 28 '26 22:04

FZs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!