Optimus Pette
Optimus Pette

Reputation: 3400

How can i unescape this query string

I am sending this request to a service:

get_store_data = Typhoeus::Request.new("http://localhost:3000/api/v1/store?service=#{(proxy_ticket.service)}&ticket=#{proxy_ticket.ticket}")

proxy_ticket.service resolves to this string "http://localhost:3000/api/v1/store". When the request is sent, This string is escaped to this:

service=http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fv1%2Fstore

The problem is that the service on the other end expects the service parameter as "http://localhost:3000/api/v1/store" how can i prevent this query string from being escaped?

Upvotes: 5

Views: 5541

Answers (2)

BroiSatse
BroiSatse

Reputation: 44715

You can't. Its the 'other side' who should decode this param (and most likely they do).

For example rails do this encoding automatically. You can check it by altering some of your actions to raise params.pretty_inspect and invoke it with extra param your/action/route?service=http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fv1%2Fstore. You will see that params include service: http://localhost:3000/api/v1/store.

If this is not working for you this mine that you need to get in touch with the other side so they implement this. There is no other way to pass urls within get urls.

Upvotes: 2

Mike H-R
Mike H-R

Reputation: 7835

The other side should be unescaping the params. If you would still like to know how to do it however here is the method uri.unescape which is used like so:

require 'uri'

enc_uri = URI.escape("http://example.com/?a=\11\15")
p enc_uri
# => "http://example.com/?a=%09%0D"

p URI.unescape(enc_uri)
# => "http://example.com/?a=\t\r"

If you ever want to quickly unescape a uri (and don't want to open a repl for some strange reason or other, like maybe it insulted your honour or something.) you can try this site

Upvotes: 6

Related Questions