- Home /
A problem when putting string variable into request URL
Hello how are you I am going to pass a variable into a URL. But "%E2%80%8B" is attached at the end of the variable.
This is my code.
[System.Serializable]
public class Param {
public string key;
public string value;
public Param(string k, string v) {
key = k;
value = v;
}
}
private string parseParam (List<Param> paramList) {
string strParam = "?";
foreach (Param param in paramList) {
strParam += param.key;
strParam += "=";
strParam += param.value;
strParam += "&";
}
return (strParam = strParam.Remove (strParam.Length - 1));
}
private IEnumerator searchCity() {
string cityQuery = searchFilter.text;
string url = "http://geodb-free-service.wirefreethought.com/v1/geo/cities";
List<Param> paramList = new List<Param> ();
paramList.Add (new Param ("namePrefix", cityQuery));
paramList.Add (new Param ("limit", "10"));
paramList.Add (new Param ("offset", "0"));
paramList.Add (new Param ("hateoasMode", "false"));
url += parseParam (paramList);
UnityWebRequest req = UnityWebRequest.Get(url);
req.SetRequestHeader ("Content-Type", "application/json;charset=UTF-8");
yield return req.SendWebRequest ();
Debug.Log (req.url);
if (req.error != null) {
Debug.Log ("Getting Location Data Failed");
} else {
Debug.Log (req.downloadHandler.text);
}
}
When I put this city name "Liverpool" instead of the variable "cityQuery", the right result shows and at this time, the URL becomes "http://geodb-free-service.wirefreethought.com/v1/geo/cities?namePrefix=Liverpool&limit=10&offset=0&hateoasMode=false" but when I put the variable "cityQuery" then the result becomes "http://geodb-free-service.wirefreethought.com/v1/geo/cities?namePrefix=Liverpool%E2%80%8B&limit=10&offset=0&hateoasMode=false" and so the result not showing.
Any help is appreciated. Please help me with this question. Thanks
Sprietsma
Have you checked this link? Did you copy Liverpool
from an external source? (formatted text editor, online, ...) If so, type the word Liverpool
by hand to avoid extra invisble characters.
Hello. Thanks for your reply. But the Liverpool string is correct, I checked the output. http://geodb-free-service.wirefreethought.com/v1/geo/cities?namePrefix=Liverpool&limit=10&offset=0&hateoas$$anonymous$$ode=false this works, but string cityQuery = "Liverpool"; "http://geodb-free-service.wirefreethought.com/v1/geo/cities?namePrefix=" + cityQuery + "&limit=10&offset=0&hateoas$$anonymous$$ode=false" this not working I think this is related to geodb-free-service.wirefreethought.com server but not sure and dont know the reason. I would like you to help me. Thanks
Before building the URL, call Debug.Log( cityQuery + " " + cityQuery.Length )
and make sure the length of the string is correct. As I said, you may have invisible characters causing your issue.
Perhaps you can just replace the trailing zero-length white space:
cityQuery.Replace("\u200B", "");
Your answer
