3

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.

2
  • share code sample. Commented Apr 10, 2020 at 13:22
  • Edited with code @mehta-rohan Commented Apr 10, 2020 at 13:26

1 Answer 1

5

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. It worked. Sometimes we miss basic concepts :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.