1

Here is my API request

public IEnumerator Login(string bodyJsonString)
{
    Debug.Log(bodyJsonString);

    UnityWebRequest req = UnityWebRequest.Post("localhost:3000/login", bodyJsonString);
    req.SetRequestHeader("content-type", "application/json");
    yield return req.SendWebRequest();
    if (req.isNetworkError || req.isHttpError)
    {
        Debug.Log(req.error);
    }
    else
    {
        Debug.Log("Form upload complete!");
    }

}

It returns an error status code 500 and on the server returns an error Unexpected token % in JSON at position 0","severity

Here is my Coroutine Call

public void submitLogin()
{

    _username = userInputField.GetComponent<InputField>().text;
    _password = passwordInputField.GetComponent<InputField>().text;

    Debug.Log("username" + _username);
    Debug.Log("password" + _password);

    string body = "{'username':'" + _username + "','password','" + _password + "'}";

    //API Call
    authChexi = new Auth();
    StartCoroutine(authChexi.Login(body));
}

Let me know if you have ideas on how to deal with my form body. Thanks

Frank Mendez
  • 546
  • 3
  • 13
  • 26
  • Maybe related: https://stackoverflow.com/a/59878957/7111561 and https://stackoverflow.com/a/59405175/7111561 – derHugo Mar 26 '20 at 07:17
  • 1
    Also I hope you escape this somehow ... what if a user types in a `'` or `}` as part of the username or in particular the password? – derHugo Mar 26 '20 at 07:33
  • 1
    Why are you manually building up a JSON string? That's almost certainly where your issue is. Use Unity's built-in JSON serializer to do that instead. – Ian Kemp Mar 26 '20 at 07:33

2 Answers2

7

So I have updated my function. I did some digging and finally solved it. My mistake was indeed manually building up a JSON. So here is my solution.

public void submitLogin()
{

    _username = userInputField.GetComponent<InputField>().text;
    _password = passwordInputField.GetComponent<InputField>().text;

    //API Call
    authChexi = new Auth();
    StartCoroutine(authChexi.Login(_username, _password));
}

Created a class userdata for my json object

public class UserData 
{
    public string username;
    public string password;
    public string email;
}

And call the API

public IEnumerator Login(string username, string password)
{
    //@TODO: call API login
    // Store Token
    // Add Token to headers

    var user = new UserData();
    user.username = username;
    user.password = password;

    string json = JsonUtility.ToJson(user);

    var req = new UnityWebRequest("localhost:3000/login", "POST");
    byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
    req.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
    req.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
    req.SetRequestHeader("Content-Type", "application/json");

    //Send the request then wait here until it returns
    yield return req.SendWebRequest();

    if (req.isNetworkError)
    {
        Debug.Log("Error While Sending: " + req.error);
    }
    else
    {
        Debug.Log("Received: " + req.downloadHandler.text);
    }

}

And now it's working like a charm!

Frank Mendez
  • 546
  • 3
  • 13
  • 26
0

You should provide a valid JSON string in the request. You should provide double quotes instead of single quotes for each attributes with the help of the escape character ("").

Try to change the methods as follows,

public IEnumerator Login(string bodyJsonString)
{
    UnityWebRequest request = new UnityWebRequest("localhost:3000/login", "POST");
    byte[] data = new System.Text.UTF8Encoding().GetBytes(bodyJsonString);
    request.uploadHandler = (UploadHandler) new UploadHandlerRaw(data);  // important
    request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
    request.SetRequestHeader("Content-Type", "application/json"); //important

    yield return request.SendWebRequest();

    if (request.isNetworkError)  // 
        Debug.Log("Error While Sending: " + request.error);
    else
        Debug.Log("Received: " + request.downloadHandler.text);
}

public void submitLogin()
{

    _username = userInputField.GetComponent<InputField>().text;
    _password = passwordInputField.GetComponent<InputField>().text;

    Debug.Log("username" + _username);
    Debug.Log("password" + _password);

    string body = "{\"username\":\"" + _username + "\",\"password\":\"" + _password + "\"}"; //important

    //API Call
    authChexi = new Auth();
    StartCoroutine(authChexi.Login(body));
}
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81