Skip to content

Newline "\n" is encoded as "%0D%0A" in query string values #449

Closed

Description

@vassilevsky

Hello 🙂

I use version 2.2.2 but the code seems to be the same in 3.0.0 as well.

I noticed that newlines \n are encoded as \r\n in query string:

 ~    ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17]
 ~    irb
irb(main):001:0> require 'http'
=> true
irb(main):002:0> HTTP::VERSION
=> "2.2.2"
irb(main):003:0> require 'httplog'
=> true
irb(main):004:0> HTTP.get('http://example.com', params: {foo: "bar\nbaz"})
D, [2018-01-15T19:25:37.641493 #66440] DEBUG -- : [httplog] Sending: GET http://example.com/?foo=bar%0D%0Abaz

This is because the params are put through Addressable::URI.form_encode, which replaces everything with \r\n:

uri.query = [uri.query, HTTP::URI.form_encode(opts.params)].compact.join("&")

I think that this behavior is incorrect. It is also unexpected. Query string values are not form values.

For example, ActiveSupport simply encodes \n:

irb(main):001:0> require 'active_support/core_ext/object/to_query'
=> true
irb(main):002:0> {foo: "bar\nbaz"}.to_query
=> "foo=bar%0Abaz"

Activity

  1. mikegee commented on Jan 16, 2018

    @mikegee
    Contributor

    Rack behaves like ActiveSupport on this. (ActiveSupport might delegate to Rack):

    Rack::Utils.build_nested_query(foo: "bar\nbaz")
    #=> "foo=bar%0Abaz"

    Looks like http is actually delegating to Addressable here:

    http/lib/http/uri.rb

    Lines 45 to 47 in c698449

    def self.form_encode(form_values, sort = false)
    Addressable::URI.form_encode(form_values, sort)
    end

    We can use Addressable differently to get the expected behavior:

    uri = Addressable::URI.new
    uri.query_values = { foo: "bar\nbaz" }
    uri.query
    #=> "foo=bar%0Abaz"
  2. added theissue type on Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions