Encoding url in Racket #106

Closed
opened 3 years ago by casouri · 3 comments
casouri commented 3 years ago (Migrated from github.com)

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?

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 (Migrated from github.com)

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"
This might work for you: parse the URI first and then re-convert it to a string: ```racket > (url->string (string->url "http://mysite.com/this place<")) "http://mysite.com/this%20place%3C" ```
otherjoel commented 3 years ago (Migrated from github.com)

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.

If that doesn’t quite cut it, you could parse the string into [`url`][1] struct with `string->url` and then do your own encoding on just the path portion. [1]: https://docs.racket-lang.org/net/url.html#%28def._%28%28lib._net%2Furl-structs..rkt%29._url%29%29
casouri commented 3 years ago (Migrated from github.com)

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.

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.
This repo is archived. You cannot comment on issues.
No Milestone
No project
No Assignees
1 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: mbutterick/pollen-users#106
Loading…
There is no content yet.