Leo Lozes
Leo Lozes

Reputation: 1388

URL Encoding and bit.ly

I have to shorten the following URL:

https://example.com/engine?key=5&currency=EUR&language=EN&user=&system=HPH&tracking=%7B%22gtm%22%3A%22GTM-WR29KDC%22%7D&navigation=%7B%22ipAddress%22%3A%2284.126.214.206%22%2C%22countryCode%22%3A%22ES%22%2C%22domain%22%3A%22example.com%22%2C%22landingPage%22%3A%22http%3A%2F%2Fexample.com%2Fen%2F%22%2C%22userAgent%22%3A%22Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_15_7%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F109.0.0.0+Safari%2F537.36+Edg%2F109.0.1518.52%22%2C%22device%22%3A%22desktop%22%7D&availability=%7B%22dateFrom%22%3A%222023-01-18T23%3A00%3A00.000Z%22%2C%22dateTo%22%3A%222023-01-20T23%3A00%3A00.000Z%22%2C%22distribution%22%3A%5B%7B%22id%22%3A1%2C%22person%22%3A%5B%7B%22id%22%3A1%2C%22type%22%3A%22Adult%22%2C%22age%22%3A30%7D%2C%7B%22id%22%3A2%2C%22type%22%3A%22Adult%22%2C%22age%22%3A30%7D%5D%7D%5D%2C%22selectedFareProperty%22%3Anull%2C%22mealPlan%22%3Anull%2C%22selectedItem%22%3A%7B%22type%22%3A%22Hotel%22%2C%22code%22%3A%2252774%22%2C%22name%22%3A%22Hotel+de+la+RIERA+Hotel%22%7D%2C%22logged%22%3Atrue%2C%22execute%22%3Atrue%2C%22fromLink%22%3Atrue%7D

It's not pretty, but it's the URL we have to redirect to an hotel availability engine, and it works when not shortened. This is the code that generates it:

  const urlParameters = new URLSearchParams(
    {
      key: key,
      currency: currency,
      language: language,
      user: user || '',
      system: system,
      tracking: JSON.stringify(tracking || {}),
      navigation: JSON.stringify(navigation || {}),
      availability: JSON.stringify(parameters)
    },
  );
  return urlParameters.toString();

The problem comes with bit.ly, because when using the shortened URL, it redirects to:

https://example.com/engine?key%3D5%26currency%3DEUR%26language%3DEN%26user%3D%26system%3DHPH%26tracking%3D%257B%2522gtm%2522%253A%2522GTM-WR29KDC%2522%257D%26navigation%3D%257B%2522ipAddress%2522%253A%252284.126.214.206%2522%252C%2522countryCode%2522%253A%2522ES%2522%252C%2522domain%2522%253A%2522example.com%2522%252C%2522landingPage%2522%253A%2522http%253A%252F%252Fexample.com%252Fen%252F%2522%252C%2522userAgent%2522%253A%2522Mozilla%252F5.0%2B%2528Macintosh%253B%2BIntel%2BMac%2BOS%2BX%2B10_15_7%2529%2BAppleWebKit%252F537.36%2B%2528KHTML%252C%2Blike%2BGecko%2529%2BChrome%252F109.0.0.0%2BSafari%252F537.36%2BEdg%252F109.0.1518.52%2522%252C%2522device%2522%253A%2522desktop%2522%257D%26availability%3D%257B%2522dateFrom%2522%253A%25222023-01-18T23%253A00%253A00.000Z%2522%252C%2522dateTo%2522%253A%25222023-01-20T23%253A00%253A00.000Z%2522%252C%2522distribution%2522%253A%255B%257B%2522id%2522%253A1%252C%2522person%2522%253A%255B%257B%2522id%2522%253A1%252C%2522type%2522%253A%2522Adult%2522%252C%2522age%2522%253A30%257D%252C%257B%2522id%2522%253A2%252C%2522type%2522%253A%2522Adult%2522%252C%2522age%2522%253A30%257D%255D%257D%255D%252C%2522selectedFareProperty%2522%253Anull%252C%2522mealPlan%2522%253Anull%252C%2522selectedItem%2522%253A%257B%2522type%2522%253A%2522Hotel%2522%252C%2522code%2522%253A%252252774%2522%252C%2522name%2522%253A%2522Hotel%2Bde%2Bla%2BRIERA%2BHotel%2522%257D%252C%2522logged%2522%253Atrue%252C%2522execute%2522%253Atrue%252C%2522fromLink%2522%253Atrue%257D

Which seems to be encoded again, and the link doesn't work.

If I try to shorten the decoded URL, it doesn't work either, because not everything is encoded:

/engine?key=5&currency=EUR&language=en&user=&system=HPH&tracking={%22gtm%22:%22GTM-WR29KDC%22}&navigation={%22ipAddress%22:%2284.126.214.206%22,%22countryCode%22:%22ES%22,%22domain%22:%22demo.pre.new.hotetec.com%22,%22landingPage%22:%22http://demo.pre.new.hotetec.com/en/%22,%22userAgent%22:%22Mozilla/5.0+(Macintosh%3B+Intel+Mac+OS+X+10_15_7)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36+Edg/109.0.1518.52%22,%22device%22:%22desktop%22}&availability={%22dateFrom%22:%222023-01-18T23:00:00.000Z%22,%22dateTo%22:%222023-01-20T23:00:00.000Z%22,%22distribution%22:[{%22id%22:1,%22person%22:[{%22id%22:1,%22type%22:%22Adult%22,%22age%22:30},{%22id%22:2,%22type%22:%22Adult%22,%22age%22:30}]}],%22selectedFareProperty%22:null,%22mealPlan%22:null,%22selectedItem%22:{%22type%22:%22Hotel%22,%22code%22:%2252774%22,%22name%22:%22Hotel+de+la+RIERA+Hotel%22},%22logged%22:true,%22execute%22:true,%22fromLink%22:true} 

I'm very likely doing something wrong here, but I don't see how to fix it. I'm using node-bitly 7.1.2 and calling bitly.shorten(myUrl).

Upvotes: 1

Views: 295

Answers (0)

Related Questions