Encode “/“ inside CallbackURL

url-schemes

#1

The CallbackAPIs automatically perform URL encoding on parameters but not for the slash character where I believe it should be encoded as %2F. Is there any way around this - a way to make an exception where I can manually encode that anyone knows of? Is this a bug?
Any hell is much appreciated.


#2

What are the “CallbackAPIs” you are referring to?

Do you mean x-callback-url?

Can you provide an example of what you have and how the issue is appearing?


#3

the only time you should need to encode the / character is when you are embedding a URL within another url. something like:

http://example.com/?ref=http%3A%2F%2Fwww.example.net%2Fpath%2Fto%2Fresource.pdf


#4

Here’s an example of what I’m trying to do

const callbackUrl = new CallbackURL('bear://x-callback-url/get-tag');
callbackUrl.addParameter("name", "foo/bar");
log(callbackUrl.getURL());

// expected:

// bear://x-callback-url/get-tag?name=foo%2Fbar&x-source=Scriptable&x-success=scriptable://x-callback-url/success&x-error=scriptable://x-callback-url/error&x-cancel=scriptable://x-callback-url/cancel

// actual:

// bear://x-callback-url/get-tag?name=foo/bar&x-source=Scriptable&x-success=scriptable://x-callback-url/success&x-error=scriptable://x-callback-url/error&x-cancel=scriptable://x-callback-url/cancel

As you can see, the slash between foo and bar isn’t escaped as I believe it should be, thereby breaking the URL. This might well be the intended behaviour, but I’m wonder if there’s a way to attach a parameter without it being escaped so I can do the escaping myself


#5

Okay, so forward slashes are perhaps a special case, but also it doesn’t seem like there’s any way to get it into the CallbackURL, including trying to push it in via the base URL. As such, I think you would have to roll your own building of the full URL.

I would expect the addParameter method to accommodate the encoding but obviously it doesn’t currently. One for @simonbs to clarify perhaps?


#6

Sorry for the late answer. CallbackURL does in fact escape values of parameters. For example, if you do the following the spaces are escaped.

let url = new CallbackURL("bear://x-callback-url")
url.addParameter("note", "This is my note")
log(url.getURL())
// bear://x-callback-url?note=This%20is%20my%20note&x-source=Scriptable&x-success=scriptable://x-callback-url/success&x-error=scriptable://x-callback-url/error&x-cancel=scriptable://x-callback-url/cancel

I might be wrong but I don’t think slashes need to be escaped in query parameters. I’ve tried opening the following URL in various ways and every time Scriptable seem to decode it just fine and I’m not doing anything special to handle slashes or any other characters.

scriptable:///run?scriptName=My%20Script&param=foo/bar

I haven’t tested with Bear yet but I’m starting to wonder if they do anything special when decoding the query parameters that might cause slashes to fail.

If anyone have more examples of URLs that cannot be parsed by other apps because of the encoding, I’d love to hear about it.