Unity3D WebRequest URL doesn't work when I add some parameter to URL string

81 Views Asked by At

I am doing basic game project for my college. I have a backend project and the project's get or post methods work correctly on postman or any browser. On the Unity side, I created 2 Inputfields (username, password) and created 1 Button (Login)

I create a c# script and define URL string and made webrequest with an IEnumerator and then StartCoroutine in the start method. This way it works fine

private string URL = "https://localhost:44310/api/users/getbyaccount?username=blablabla&&password=blablabla";

// Start is called before the first frame update
void Start()
{
    StartCoroutine(GetResult());
}


IEnumerator GetResult()
{
    using (UnityWebRequest request = UnityWebRequest.Get(URL))
    {
        yield return request.SendWebRequest();

        if (request.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log(request.error);
        }
        else
        {

            string message;
            message = request.downloadHandler.text;

            messageBoxContainer.gameObject.SetActive(true); 
            messageBoxText.text = message;
        }
    }
}

But I want to create string URL with username and password come from inputfields. So, I created a button and wrote a method when click add the parameters(username and password) to URL string. But doesn't work.

public Image messageBoxContainer;

public TMP_Text _userName;
public TMP_Text _password;

public TextMeshProUGUI messageBoxText;


private string URL;

// Start is called before the first frame update
void Start()
{
   // StartCoroutine(GetResult());
}


IEnumerator GetResult()
{
    using (UnityWebRequest request = UnityWebRequest.Get(URL))
    {
        yield return request.SendWebRequest();

        if (request.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.Log(request.error);
        }
        else
        {

            string message;
            message = request.downloadHandler.text;

            messageBoxContainer.gameObject.SetActive(true); 
            messageBoxText.text += message;
        }
    }
}


public void CreateUrl()
{
   
    URL ="https://localhost:44310/api/users/getbyaccount?username=" + _userName.text + "&&password=" + _password.text;

    messageBoxText.text = URL;

    StartCoroutine(GetResult());
}

How can I add the parameters to URL string?

1

There are 1 best solutions below

0
On

I found the solution!

I should TMP_InputField instead TMP_Text.

It works.