API add new ssh public key "ssh-rsa xxx+yyy+zzz me@me" becomes "ssh-rsa xxx yyy zzz me@me"

April 5, 2014 3.9k views
Hi. I'm using Ruby and DO API to create my public ssh key like this RestClient.get URI.encode("https://api.digitalocean.com/ssh_keys/new/?name=[ssh_key_name]&ssh_pub_key=[ssh_public_key]&client_id=[client_id]&api_key=[api_key]")) The key is something like this "ssh-rsa xxx+yyy+zzz me@me" then after the call done, the key will be stored like this "ssh-rsa xxx yyy zzz me@me". The "+" was replaced by space. So I have to use web interface to edit the key. How could I do this correctly? Thank you.
2 Answers
URI.encode doesn't seem to be encoding the SSH string properly, it should be escaping the plus character. You should use CGI.escape instead, but keep in mind it should only be used on the ssh key itself otherwise it would escape the whole string rendering it unusable. There are a few gems that will escape the whole URI correctly. One of them being http://rubydoc.info/gems/addressable/2.2.4/Addressable/URI#encode-class_method

The following links might also help:

@Kamal Masser: Thank a lot! CGI.escape helps me encode the ssh_key correctly.
Have another answer? Share your knowledge.