I need to append params to my query, but I always get different results. I tried a few ways but without success. Check code below.
First check my code which work without new URL (I need to use new URL).
let url = `${getBaseUrl()}/myUrl/search?surnames=${allData.join('%0A')}`
This works well, but when I use new URL:
let url = new URL('myUrl/search' , getBaseUrl())
url.searchParams.append('surnames' , allData);
The code above doesn't work, and I don't know the reason why?
I tried to inspect url and see different
Works: search?surnames=smith%0Ajordan
Doesn't work: search?surnames=smith%250Ajordan
The only difference is inside between "smith" and "jordan"
With %0A work
With %250A doesn't work
Not sure how this was generated.
The original % is being encoded as %25, so %0 becomes %250.
Try using an unencoded line feed (\n):
const
allData = ['smith', 'jordan'],
getBaseUrl = () => 'https://localhost:8080',
url = new URL('myUrl/search' , getBaseUrl());
url.searchParams.append('surnames', allData.join('\n'));
console.log(url); // https://localhost:8080/myUrl/search?surnames=smith%0Ajordan
Related
I am trying to add search and page to my url for searching and pagination on a page.
const urlParams = new URLSearchParams(window.location.search);
if(!urlParams.has('search'){
urlParams.append('search', question);
}
if(!urlParams.has('page'){
urlParams.append('page', pageIndex);
}
This appears to do nothing to the actual url.
But when I call urlParams.toString()
then I can see that they have been added, but they are not in the actual url in the browser.
I'm using Chrome 107, so it should support it.
Am I missing something?
The documentation has not helped me so far.
Of course it does nothing with the actual URL, you are creating a URLParameters Object and updating it. what you are missing is:
window.loacation.search = urlParams.toString()
it will change the query string in the browser URL and reloads the page.
if you are not interested in reloading the page, you can use history DOM object
let url = new URL(window.location.href);
if(!(url.searchParams.has('search'))){
url.searchParams.append('search', question);
}
if(!(url.searchParams.has('page'))){
url.searchParams.append('page', pageIndex);
}
history.pushState({},'',url.href);
finally, if you want to update the page and search params anyway, you can use the url.searchParams.set() method, like:
url.searchParams.set('page', pageIndex);
it will append the parameter if it does not exist, and will update it if it does, without throwing exceptions.
You can try this way:
First, retrieve the current path. Then, append the urlParams to the retrieved path and use history.pushState() to set the new URL.
const question = "the question";
const pageIndex = 3;
const urlParams = new URLSearchParams(window.location.search);
if (! urlParams.has('search')) {
urlParams.append('search', question);
}
if (! urlParams.has('page')) {
urlParams.append('page', pageIndex);
}
const path = window.location.href.split('?')[0];
const newURL = `${path}?${urlParams}`;
history.pushState({}, '', newURL);
Sources:
MDN - URLSearchParams
MDN - History.pushState()
Get the URL without query string
Hi I have a case a URL simillar to this:
https://linkhere?response-content-disposition=inline; filename="placeholder.jpg"; filename*=UTF-8''placeholder.jpg response-content-type=image/jpeg&X-Amz-Algorithm=....sometextcontinueshere
I am trying to decode it like this and I need to take the filename
const decodedUrl = decodeURIComponent("linkhere")
const urlParams = new URLSearchParams(decodedUrl);
const filename = urlParams.get('filename');
console.log(decodedUrl)
But for some reason does not work the decoding properly, do you guys have any idea?
There is nothing built in that is magically going to get the filename since it is not a querystring paramter. The easiest thing you can do it change whatever is building this to have valid querystring parameters so you can parse it.
With what you have, you will need to read the one parameter that has the file name in it. After that you are going to have to parse out the filename from that string.
Basic idea:
var str = `https://linkhere?response-content-disposition=inline; filename="placeholder.jpg"; filename*=UTF-8''placeholder.jpg response-content-type=image/jpeg&X-Amz-Algorithm=....sometextcontinueshere`;
const url = new URL(str);
const parts = url.searchParams.get('response-content-disposition').split(/;\s/);
const fileNameParam = parts.find(x => x.startsWith('filename=')).match(/"([^"]+)"/)[1];
console.log(fileNameParam);
Having this kind of endpoing:
https://hey.com/p1/m/p2
I want to get rid of the https://hey.com and get only the /p1/m/p2 part on the Pre-request Script. I know I can do it using request.url.replace(/^.*\/\/[^\/]+/, ''), getting the desired output /p1/m/p2.
Is there any other way to do it without using replace or regex? Something like request.url.pathname (which is not working, obviously).
The URL above is just an example, the endpoints and urls will vary.
Bear in mind that I'm using the Pre-request Script on the Postman environment and some things may not work.
Thank you so much.
You should be able to use the URL browser API to construct a URL object.
(new URL(request.url)).pathname
If you're using the desktop version, you can use the built-in node.js API.
var URL = require('url');
URL.parse(request.url).pathname
You can try something like this
var fullUrl = request.url.split('/');
fullUrl.splice(0,3);
var url = a.join('/');
console.log(url);
This is related to this thread, but it's a new problem I encountered. I am trying to add an image to a table cell in HTML, and I'm using Firebase Storage in combination with Firebase Database for this. The idea is to get the image from a gs://xxxxxxxx path in the database and use it in the js, but I get this error in the console:
Firebase Storage: Invalid argument in refFromURL at index 0: Expected full URL but got a child path, use ref instead."
So it seems that the url_ul path is not working. If, for example, I introduce the 'gs://xxxxxxxx' instead of url_ul, it works like a charm. And if I read the value url_ul as a text, I get that path. However, it doesn't work if I have that variable in refFromURL('url_ul')
I have the following js code for this:
var rootRef = firebase.database().ref().child("Test");
var storage = firebase.storage();
rootRef.on("child_added", snap => {
var headline = snap.child("headline").val();
var url_ul = snap.child("imageUrl").val();
var storageRef = storage.refFromURL('url_ul').then(function(url) {
var test = url;
document.querySelector('img').src = test;
}).catch(function(error) {});
$("#table_body").append("<tr><td>" + headline + "</td><td><img src='test' alt='' border=3 height=100 width=100></td></tr>");});
where the database looks like this:
So there is a gs://xxxxx path, which, according to Firebase documentation, should work fine:
// Create a reference from a Google Cloud Storage URI
var gsReference = storage.refFromURL('gs://bucket/images/stars.jpg')
Any idea what is wrong here?
To make things more clear:
This line works perfectly fine:
var storageRef = storage.refFromURL("gs://nameofapp.appspot.com/armonii_culturale.png").getDownloadURL().then(function(url) ...
But this one doesn't
var storageRef = storage.refFromURL("url_ul").getDownloadURL().then(function(url) ...
And it's strange because "url_ul" should contain the same value.
Ok this should be a relatively simple one. Apologies i am an iOS developer usually trying to understand Javascript.
I have a PFFile that is being saves into a PFObject from iOS.
I now need to send the URL of the image to a web service, which i am trying to do using an afterSave method on Cloud Code.
I have the object being fetched, and i can see the file contained within it. But i can't work out how to access the URL value.
I have tried various approaches, and i'm sure it's me not understanding how to access the value on an object, nested in another object. I've listed what i mostly tried below.
I have everything else working apart from this so help would be greatly appreciated.
Thanks
Gareth
var image = request.object.get("userPhoto")
console.log(image);
var imageURL = image.url
var imageURL = image.'url'
var imageURL = image.(url)
var imageURL = image.('url')
console.log(imageURL);
The method for accessing the URL of the Parse.File object is:
var imageURL = image.url();
Per the docs here: https://parse.com/docs/js_guide#files-retrieving