0

I'm trying to combine a url and string at runtime and then call it.

public static Uri Append(this Uri uri, params string[] paths)
    {
        return new Uri(paths.Aggregate(uri.AbsoluteUri, (current, path) => string.Format("{0}/{1}", current.TrimEnd('/'), path.TrimStart('/'))));
    }

var url = new Uri("https://127.0.0.1:2999/liveclientdata/playerscores?summonerName=").Append(_PlayerName).AbsoluteUri;

However when I call it, this error is returned:

Failed the request: HTTP/1.1 400 Bad Request

The url looks like this

https://127.0.0.1:2999/liveclientdata/playerscores?summonerName=/%22KidKiwi91%22

I know the error is caused by the concatenation of the url and string because it I make it all one url and not combine them in runtime it works.

Other things i've tried:

string url = "urlgoeshere=" + playername;
string url = UnityWebRequest.EscapeURL("urlgoeshere" + playername);

string url_q = "urlgoeshere=" + playername;
var url=new Uri(url_q);

It's called using this

private IEnumerator GetJSON(string url, System.Action<string> callback)
    {
        failed = false;

        //Debug.Log(url);
        using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
        {
            webRequest.certificateHandler = new BypassCertificate();

            yield return webRequest.SendWebRequest();

            string error = webRequest.error;

            if (error != null)
            {
                Debug.Log("Failed the request: " + error);
                failed = true;
            }
            else
            {
                callback?.Invoke(webRequest.downloadHandler.text);
                //Debug.Log(webRequest.downloadHandler.text);
            }
        }
    }

Any ideas?

Thanks

7
  • 3
    Does this answer your question? Path.Combine for URLs? Commented Sep 22, 2020 at 6:55
  • No, i've tried half of them and same error on all of them Commented Sep 22, 2020 at 7:03
  • Have you tried debugging the exact final URL and compare it to one that works that you hardcode? In other words: Are you sure the issue is with the code or is your URL incorrect at all? A 400 means that the server was reached correctly but doesn't understand the request ... can you post a URL that is working correctly when you hardcode it and tell us what exactly all your variables hold? I'm pretty sure there is an / too much and it should rather be https://127.0.0.1:2999/liveclientdata/playerscores?summonerName=%22KidKiwi91%22 Commented Sep 22, 2020 at 7:06
  • @derHugo Working url: 127.0.0.1:2999/liveclientdata/… Concated url: 127.0.0.1:2999/liveclientdata/…" Looks like it's adding the quotes Commented Sep 22, 2020 at 7:14
  • That would probably be the %22 .. how exactly does your _PlayerName look like? you could probably avoid it by using _PlayerName.Trim('"') (it is ' " ' a bit hard to see ;) ) Commented Sep 22, 2020 at 7:17

1 Answer 1

0

Try this

string url_q = "https://127.0.0.1:2999/liveclientdata/playerscores?summonerName=" + 
playername;

var url=new Uri(url_q);
Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately the same error
pls provide the remaining code, where did you want to put this url?
Added to the original post
what value you got in url variable?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.