Question

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

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.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

@Kamal Masser: Thank a lot! CGI.escape helps me encode the ssh_key correctly.

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 <a href=“http://rubydoc.info/gems/addressable/2.2.4/Addressable/URI#encode-class_method”>http://rubydoc.info/gems/addressable/2.2.4/Addressable/URI#encode-class_method</a> <br> <br>The following links might also help: <br> <br><ul><li><a href=“http://stackoverflow.com/questions/2824126/whats-the-difference-between-uri-escape-and-cgi-escape”>http://stackoverflow.com/questions/2824126/whats-the-difference-between-uri-escape-and-cgi-escape</a></li><li><a href=“http://ruby-doc.org/stdlib-1.9.3/libdoc/cgi/rdoc/CGI.html#method-c-escape”>http://ruby-doc.org/stdlib-1.9.3/libdoc/cgi/rdoc/CGI.html#method-c-escape</a></li></ul>