Escape processing when creating a URL in Ruby

If you create a URL with query parameters in a string URL such as https: // xxxx, the query parameters need to be converted to a special form.

You need to execute encodeURIComponent in JavaScript. This makes + to% 2B and & to% 26.

Basic

Ruby uses CGI.escape.

require 'cgi'

CGI.escape('+ &') # => "%2B+%26"

Example of use

Try constructing the URL using CGI.escape. There are many things that can be improved, such as using hashes, but here is a simple pattern.

"https://samole.com?key={CGI.escape(value)}"

Only value is escaped, thinking that it is unlikely to use special characters for key.

If you also want to escape key, You can refer to https://github.com/rails/rails/blob/3-0-stable/activesupport/lib/active_support/core_ext/object/to_query.rb.

Other references

Recommended Posts

Escape processing when creating a URL in Ruby
(Ruby on Rails6) Creating data in a table
Creating a calendar using Ruby
Multiplication in a Ruby array
Sorting hashes in a Ruby array
Encoding when getting in Windows + Ruby
Implement a gRPC client in Ruby
Escape processing when searching for LIKE
About what I did when creating a .clj file in Clojure
A memorial service for the library used when competing in Ruby
(Ruby on Rails6) Creating a database and displaying it in a view
Be careful when omitting return in Ruby
I tried a calendar problem in Ruby
When seeking multiple in a Java array
[Creating] A memorandum about coding in Java
Precautions when specifying the URL in render
Creating a Servlet in the Liberty environment
Do not return when memoizing in Ruby
Creating a matrix class in Java Part 1
Be careful when setting the class name when creating a document-based app in Swift
How to start a subscript from an arbitrary number in Ruby iterative processing
Convert to a tag to URL string in Rails
Behavior when wild card (**) is specified in ruby
Creating a browser automation tool with Ruby + Selenium
Create a simple batch processing framework in Eclipse.
Code to escape a JSON string in Java
A note when you want Tuple in Java
I made a Ruby extension library in C
Draw a too beloved Mandelbrot set in Processing
[Ruby] When adding a null constraint to a table
Create a native extension of Ruby in Rust
Keyboard when tapping a text field in WebView
Ruby on Rails Incorrect string value error resolution when posting a form in Japanese
Guidelines for writing processing when a value exists / does not exist in Java Optional
[Ruby] Iterative processing
Processing when an ID that does not exist in the database is entered in the URL
Class in Ruby
Heavy in Ruby! ??
Ruby Iterative Processing
Do something like a JS immediate function in Ruby
Count the number of occurrences of a string in Ruby
Creating a Cee-lo game with Ruby 4th Creating a game progress process
Things to watch out for when creating a framework
Calculate the difference between numbers in a Ruby array
[Ruby / Rails] Set a unique (unique) value in the class
What impressed me as a beginner in writing Ruby
How to launch another command in a Ruby program
Addition of variables in iterative processing in a while statement
A memorandum when creating a REST service with Spring Boot
What I learned when building a server in Java
Set the root URL when starting tomcat in intelliJ
Correspondence when Ruby version does not switch in rbenv
[Programming complete] ยง5 Create a review management app in Ruby
Call a program written in Swift from Processing (Java)
Get the URL of the HTTP redirect destination in Ruby
Use Coveralls with GitHub Actions in a Ruby repository
[Ruby] Get in the habit of using the dup method when making a copy of a string variable