By tungnsinf
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.
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!
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>
@Kamal Masser: Thank a lot! CGI.escape helps me encode the ssh_key correctly.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.