如何使用UnityWebRequest发布api调用发布数据

问题描述 投票:-1回答:1

这是我的API请求

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!");
    }

}

它返回错误状态代码500,并且在服务器上返回位置0“,”严重性“中的JSON中的错误意外令牌%>

这是我的协程电话

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));
}

让我知道您是否对如何处理我的表单有任何想法。谢谢

[这是我的API请求,公共IEnumerator登录(string bodyJsonString){Debug.Log(bodyJsonString); UnityWebRequest req = UnityWebRequest.Post(“ localhost:3000 / login”,bodyJsonString); ...

c# json unity3d unity-webgl unitywebrequest
1个回答
0
投票

所以我已经更新了我的功能。我做了一些挖掘,终于解决了。我的错误确实是手动建立JSON。所以这是我的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.