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

Cypras
  • 478
  • 7
  • 19
  • 3
    Does this answer your question? [Path.Combine for URLs?](https://stackoverflow.com/questions/372865/path-combine-for-urls) – Thomas Sep 22 '20 at 06:55
  • No, i've tried half of them and same error on all of them – Cypras Sep 22 '20 at 07: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` – derHugo Sep 22 '20 at 07:06
  • @derHugo Working url: https://127.0.0.1:2999/liveclientdata/playerscores?summonerName=KidKiwi91 Concated url: https://127.0.0.1:2999/liveclientdata/playerscores?summonerName="KidKiwi91" Looks like it's adding the quotes – Cypras Sep 22 '20 at 07: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 ;) ) – derHugo Sep 22 '20 at 07:17
  • _PlayerName is just a standard string KidKiwi91, it seems by doing this on the concatenated string url2 = url2.Replace("\"", string.Empty).Trim(); it works. – Cypras Sep 22 '20 at 07:23
  • check my reply to this and if help delete this post: https://stackoverflow.com/questions/45470315/unity-3d-call-post-api-with-json-request/45476691#45476691 – joreldraw Sep 22 '20 at 12:52

1 Answers1

0

Try this

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

var url=new Uri(url_q);
Fighter
  • 29
  • 1
  • 9