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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With