I have a URL string where there are line breaks and carriage returns. E.g.
http://xyz.com/hello?name=john&msg=hello\nJohn\n\rgoodmorning¬e=last\night I went to \roger
Where my actual msg
string is:
hello
john
goodmorning
and note
string is
last\night I went to \roger
In order to send it correctly I have to urlencode this
http://xyz.com/hello?name%3Djohn%26msg%3Dhello%5CnJohn%5Cn%5Crgoodmorning%26note%3Dlast%5Cnight%20I%20went%20to%20%5Croger
But this encode mess up \n and \r. While I expect \n should get converted to %0A and \r to %0D
I am writing code is ruby. And I tried to take help of Addressable::URI
but no help yet. Other way could be replacing \n and \r manually to %0A and %0D respectively. But that replacement can replace valid character such as last\night
to last%0Aight
that I don't want. Can anyone suggest a better solution? Thanks.
What about CGI::escape
You need to only encode the parameters though.
url = "http://xyz.com/hello?"
params = "name=john&msg=hello\nJohn\n\rgoodmorning¬e=last\night I went to \roger"
puts "#{url}#{CGI::escape(params)}"
# => "http://xyz.com/hello?name%3Djohn%26msg%3Dhello%0AJohn%0A%0Dgoodmorning%26note%3Dlast%0Aight+I+went+to+%0Doger"