mbutterick / pollen-users

please use https://forums.matthewbutterick.com/c/typesetting/ instead
https://forums.matthewbutterick.com/c/typesetting/
52 stars 0 forks source link

Encoding url in Racket #106

Closed casouri closed 3 years ago

casouri commented 3 years ago

I have a bunch of urls that contain Unicode, and I need to encode them. The problem is, encode-uri provided in net/uri-codec encodes reserved characters, so https://mysite.com becomes https%3A%2F%2Fmysite.com. What I really want is to not encode reserved characters and only encode non-ascii characters. I looked around and couldn't find a function that suits my needs. I could roll my own, but surely someone has figured it out, right?

otherjoel commented 3 years ago

This might work for you: parse the URI first and then re-convert it to a string:

> (url->string (string->url "http://mysite.com/this place<"))
"http://mysite.com/this%20place%3C"
otherjoel commented 3 years ago

If that doesn’t quite cut it, you could parse the string into url struct with string->url and then do your own encoding on just the path portion.

casouri commented 3 years ago

I thought I replied and thanked you but apparently not. I hope this isn't too late ;-) Thanks! Using url->string with string->url works great.