'encodeURI(JSON.stringify()) showing %255B in URL

I am trying to pass along a queryParam in Angular that consists of an array of objects as such fooArray = [{foo: 'bar', foo: false}, {foo: 'bar', foo: false}]. In the URL queryParam I am receiving the following: %255B when using encodeURI(JSON.stringify(this.fooArray))

I have tried using encodeURI(JSON.stringify()) to encode the array into the queryParam and JSON.parse(decodeURIComponent()) to retrieve the param

fooArray = [{foo: 'bar', foo: false}, {foo: 'bar', foo: false}]

fooParam: encodeURI(JSON.stringify(this.fooArray))

JSON.parse(decodeURIComponent(params["fooParam"]))



Solution 1:[1]

I was running into this problem, and what I discovered was that somewhere along the chain of my code, I was running .toString(), which runs a encodeURI on my string/url.

I was essentially double encoding my search params without knowing it. If you're running .toString() on an instance of URL or URLSearchParams class, then you don't need to manually run encodeURI or encodeURIComponent

Testing For Double Encoding

A quick way to learn if you're double encoding a string is to setup a quick example test run in your javascript developer tools. Visualizing this help me a lot:

const searchParams1 = '?status=open&created_by=bobby&date';
const url1 = "http://example.com/" + searchParams1;
const searchParamsString1 = new URL(exampl1).toString() // NOTE: `toString()` runs encode on the string for you!

const searchParams2 = '?status=open&date_filter={"by":"created","qualifier":"between","value":["2022/04/01","2022/04/02"]}';
const example2 = "http://example.com/" + searchParams2;
const searchParamsString2 = new URL(example2).toString();

// encoded 1x
searchParamsString2 ===
  "http://example.com/?status=open&date_filter={%22by%22:%22created%22,%22qualifier%22:%22between%22,%22value%22:[%222022/04/01%22,%222022/04/02%22]}";

// encoded 2x (visual)
const example2DoubleEncoded = encodeURI(
  "http://example.com/?status=open&date_filter={%22by%22:%22created%22,%22qualifier%22:%22between%22,%22value%22:[%222022/04/01%22,%222022/04/02%22]}"
);
example2DoubleEncoded ==="http://example.com/?status=open&date_filter=%7B%2522by%2522:%2522created%2522,%2522qualifier%2522:%2522between%2522,%2522value%2522:%5B%25222022/04/01%2522,%25222022/04/02%2522%5D%7D";



Solution 2:[2]

This should do the trick.

Add-Type -AssemblyName UIAutomationClient

$MyProcess = Get-Process | where { $_.MainWindowTitle -like "ccTest*" }
if ($null -ne $MyProcess) { 
    # if Minimized make it Normal
    $ae = [System.Windows.Automation.AutomationElement]::FromHandle($MyProcess.MainWindowHandle)
    $wp = $ae.GetCurrentPattern([System.Windows.Automation.WindowPatternIdentifiers]::Pattern)
    if ($wp.Current.WindowVisualState -eq 'Minimized') {
        $wp.SetWindowVisualState('Normal') 
    }
    # execute needed function
}
else {
    start-process "C:\Program Files (x86)\Test\ccTest.exe" -WindowStyle Normal
    # execute needed function
}

References:

Maximize window and bring it in front with powershell

Get window state of another process

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2