0

My website has a search form where someone can search a URL beginning with http:// like this:

https://www.google.com

which should then be encoded and appended as a query parameter value like this:

localhost:4000/api/https%3A%2F%2Fwww.google.com

When I run it (above) locally, it works, but when deployed (below):

https://api.mysite.com/search/api/https%3A%2F%2Fwww.google.com%2F

=> returns 404.

If I type this in:

http://localhost:4000/api/https://www.google.com

I get this error:

Phoenix.Router.NoRouteError at GET /api/v1/https://www.google.com
no route found for GET /api/v1/https:/www.google.com (ExternalPing.Router)

I'm not sure if these are related. What is the correct way to append a url as a query parameter value?

I have already tried encoding with URI.encode and URI.encode_www_form but they didn't resolve this

matcha
  • 81
  • 1
  • 9
  • I think the answer here will work for you, even though they're not using routing (Check out the built-in function encodeURIComponent(str) and encodeURI(str).): http://stackoverflow.com/questions/332872/encode-url-in-javascript – Chad McGrath Sep 16 '16 at 19:56
  • Are you sure `localhost:4000/api/https%3A%2F%2Fwww.google.com` doesn't work? It should work. Also, you can generate that using `URI.encode_www_form`: `"http://localhost:4000/" <> URI.encode_www_form("https://www.google.com") #=> "http://localhost:4000/https%3A%2F%2Fwww.google.com"`. – Dogbert Sep 16 '16 at 20:11
  • localhost:4000/api/https%3A%2F%2Fwww.google.com does work it's when it's deployed it doesn't work, something like this: https://api.mysite.com/search/api/https%3A%2F%2Fwww.google.com%2F – matcha Sep 18 '16 at 18:18

1 Answers1

0

Now you haven't posted your server code, so I am just going to assume here.

I think the problem is that you didn't encode the second string, since it contains / in the url you have problems.

The url is: http://localhost:4000/api/https://www.google.com

The server will interpret it wrong. So you are asking for a route called:

/api/https:/

With a parameter called /www.google.com

You need to encode the query string.

But again this is guessing since I have no idea how your server looks.

I just tried calling an endpoint at my iis server with a unencoded url as a parameter, and this is what it gave me back:

<Error>
    <Message>The request is invalid.</Message>
</Error>
Kristian Barrett
  • 3,574
  • 2
  • 26
  • 40