1

I am trying to use the League of Legends API and request data on a certain user. I use the line

    var user = getUrlVars()["username"].replace("+", " ");

to store the username. However, when I do the XMLHttpRequest with that username, it'll put %20 instead of a space.

y.open("GET", "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/"+user, false);

Edit: When I run this code with a user that has no space in their name it works, however when they have a space in their name it says the user is undefined.

For example, if I was looking for the user "the man", it would do a get at

https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/the%20man

But the correct request URL is

https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/the man
ghadams
  • 41
  • 6

3 Answers3

4

When you're creating a URL, you should use encodeURIComponent to encode all the special characters properly:

y.open("GET", "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/"+encodeURIComponent(user), false);
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

What you're experiencing is correct behaviour and is called URL encoding. HTTP requests have to conform to certain standards. The first line is always made up of three parts delimited by a space:

  1. Method (GET, POST, etc.)
  2. Path (i.e. /api/lol/na/v1.4/summoner/by-name/the%20man)
  3. HTTP version (HTTP/1.1, HTTP/1.0, etc.)

This is usually followed by HTTP headers which I'll leave out for the time being since it is beyond the scope of your question (if interested, read this https://www.rfc-editor.org/rfc/rfc7230). So a normal request looks like this:

GET /api/lol/na/v1.4/summoner/by-name/the%20man HTTP/1.1
Host: na.api.pvp.net
User-Agent: Mozilla
...

With regards to your original question, the reason the library is URL encoding the space to %20 is because you cannot have a space character in the request line. Otherwise, you would throw off most HTTP message parsers because the man would replace the HTTP version line like so:

GET /api/lol/na/v1.4/summoner/by-name/the man HTTP/1.1
Host: na.api.pvp.net
User-Agent: Mozilla
...

In most cases, servers will return a 400 bad request response because they wouldn't understand what HTTP version man refers to. However, nothing to fear hear, most server-side applications/frameworks automatically decode the %20 or + to space prior to processing the data in the HTTP request. So even though your URL looks unusual, the server side will process it as the man.

Finally, one last thing to note. You shouldn't be using the String.replace() to URL decode your messages. Instead, you should be using decodeURI() and encodeURI() for decoding and encoding strings, respectively. For example:

var user = getUrlVars()["username"].replace("+", " ");

becomes

var user = decodeURI(getUrlVars()["username"]);

This ensures that usernames containing special characters (like / which would be URL encoded as %2f) are also probably decoded. Hope this helps!

Community
  • 1
  • 1
Nadeem Douba
  • 982
  • 8
  • 8
1

Actually there are no "spaces" in the summoner names on Riot's side. So:

https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/the man

Becomes:

https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/theman

Have a look at this: https://developer.riotgames.com/discussion/community-discussion/show/jomoRum7

I am unsure how + are handled (in fact I don't think you're able to have a + in your name). All you have to do is remove the spaces.

For "funny" characters, just request them with the funny character in them, and Riot returned it fine.

https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/Trøyer?api_key=<insert your own>

will auto correct to

https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/Tr%C3%B8yer?api_key=<insert your own>

and you generally don't even have to decode it. (I used JS as my language to fetch it, if you use something else your results may require the decoded value)

Reenen
  • 128
  • 9