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);
Related
I have a search module that isn't working because it adds parameters to the URL.
For instance this URL works....
https://dev.berry.com/?s=pineapple
but the URL that is given is...
https://dev.berry.com/?s=pineapple**&et_pb_searchform_submit=et_search_proccess&et_pb_include_posts=yes&et_pb_include_pages=yes**
is there javascript that will eliminate from the "&" onward? That way whatever the search term is (pineapple, Tokyo, sunshine) it will work.
To begin, there should be no error with passing multiple parameters, so if you can, check the backend to fix this error rather than relying on front-end validation
That being said, you might want to checkout the URL module.
const url = new URL("https://dev.berry.com/?s=pineapple**&et_pb_searchform_submit=et_search_proccess&et_pb_include_posts=yes&et_pb_include_pages=yes**");
const search = url.searchParams.get("s");
url.search = "?s=" + search;
console.log(url.toString());
I am building a web app and I am using Firebase to store my user's data in Cloud Firestore. There is a page on my web app that allows users to view their documents from Cloud Firestore. I would like to add a query parameter to the end of my URL on view.html so I can take that query parameter value and use it to search for a document.
I have been searching online to find possible solutions. So far I have come across a few videos on the topic, but they haven't been going into the depth I have been needing. For example, this video shows how to add and get query parameters from a URL, but it only shows how to log those changes in the console. How would I make that my URL?
I've also be browsing Stackoverflow for solutions. This Stackoverflow post asks a similar question, however, many of the solutions in the answers causes view.html to reload on a loop. Why would this be, and if this is a possible solution, how would I stop this from happening.
How would I go about appending and fetching URL query parameters in Javascript?
You say you want to do this in javascript, so I assume the page itself is building/modifying a link to either place on the page or go to directly via javascript.
In javascript in the browser there is the URL object, which can build and decompose URLs
let thisPage = new URL(window.location.href);
let thatPage = new URL("https://that.example.com/path/page");
In any case, once you have a URL object you can access the parts of it to read and set the values.
Adding a query parameter uses the searchParams attribute of the URL, where you can add parameters with the .append method — and you don't have to worry about managing the ? and & … the method takes care of that for you.
thisPage.searchParams.append('yourKey', 'someValue');
This demonstrates it live on this page, adding search parameters and displaying the URL at each step:
let here = new URL(window.location.href);
console.log(here);
here.searchParams.append('firstKey', 'theValue');
console.log(here);
here.searchParams.append('key2', 'another');
console.log(here);
I have solved this issue in the simplest way. It slipped my mind that I could link to view.html by adding the search parameter to the URL. Here's what I did:
On index.html where I link to view.html, I created the function openViewer();. I added the parameter to the end of URL href.
function openViewer() {
window.location.href = `view.html?id={docId}`;
}
Then on view.html, I got the parameter using URLSearchParameters like so:
const thisPage = new URL(window.location.href);
var id = thisPage.searchParams.get('id');
console.log(id)
The new URL of the page is now "www.mysite.com/view.html?id=mydocid".
You can try to push state as so in the actual view.html
<script>
const thisPage = new URL(window.location.href);
window.history.pushState("id","id",thisPage);
</script>
I have a long list of urls to be modified.
For example the url is:
https://somedomain.com/?pid=24787CL22311&source=linkkit&url=https%3A%2F%2Fwww.demo.com
So in the above url I want to add subid=1233 just before the &source=linkkit
like this:
https://some.com/?pid=24787CL22311&subid=1233&source=linkkit&url=https%3A%2F%2Fwww.demo.com
I tried with append, set and replace but did not gave the specific result
Any suggestions ?
You can do something like:
let url = 'https://some.com/?pid=24787CL22311&subid=1233&source=linkkit&url=https%3A%2F%2Fwww.demo.com';
url = url.replace('&source=', '&subid=1233&source=')
I am trying to append some variable in my Liferay URL from which i am calling a serveResorce now the problem is that i want to append some value with it ,the value should be $('#id).val() with that URL ,how to do that because when i am hardcoding the value t the url then it is working but i want that a value which can be dynamic will be there,How to do that ,like i am posting my code
LULU
This is the URL which is working fine and the 7 is going to the server but i want to place a $('#id).val() in the place of the 7 ,how to do that somebody please help
You replace +7+ with this:
+ document.getElementById('id').value +
A good practice in this scenario would be creating URL through Liferay's javascript API as following:
On click event call to javascript method:
LULU
Javascript method:
function onClickLULU(){
var consultantID = document.getElementById('LULU').value;
var companyName = document.getElementById('LULUComp').value;
var resourceURL = Liferay.PortletURL.createResourceURL();
resourceURL.setParameter("consultantID", consultantID);
resourceURL.setParameter("companyName", companyName);
resourceURL.setParameter("type", "createExcelForConsultant");
location.href = resourceURL;
}
Reference: Working with Liferay URLs
What is a good way to obtain the end URL if given a URL that is being forwarded to another URL?
For example, if I had the shortened URL: http://bit.ly/900913, what is a good way to determine that this ultimately forwards to http://www.google.com?
I'm using javascript. I'm unsure if this can be done somehow using jQuery (doubtful since the end URL probably isn't returning jsonp content) or if there is some kind of web service that I can use.
Thanks!
For bit.ly specifically, you can use the bit.ly API to make a JSONP call using JavaScript to expand the bit.ly URL(s) in question.
Specifically, you'd use the v3/expand call.
Pseudo-code:
var bitlyurl = "http://bit.ly/900913";
$.getJSON("http://api.bitly.com/v3/expand?shortUrl=" + encodeURIComponent(bitlyurl)+"&apikey=...&callback=?", function( bitlydata ){
var endurl = bitlydata.data.expand[0] //looks like this is where the end URL would point
});
Alternately, you could follow the URL on your own server, and use AJAX to check it's values.
So, you'd pass it a URL ($.get("/follow?url="+bitlyurl,function(data){var endurl = data.Location;});, and make a HEAD call to the URL to see where the Location points.
Here's the basics of how you'd do it in PHP:
<?php
$headers = get_headers($_GET["url"],1);
echo json_encode($headers);
?>
Just for fun, I implemented a live end-point on App Engine to check where a URL points. Feel free to use it! The base URL is followtheredirect.appspot.com, and it requires a url parameter and a callback parameter, and returns a location key on the resulting object, when successful.
Sample code:
$.getJSON("http://followtheredirect.appspot.com/?url="+encodeURIComponent('http://bitly.com/hhN7Ol')+"&callback=?",function(data){
var location = data.location;
});
Let me know if you find any bugs :) it might be a bit messy...
Bitly provides a preview service. If you visit http://bit.ly/900913- (notice the hyphen at the end), you'll get a response with the full URL.