UnityWebRequest POST 未发送正文

UnityWebRequest POST not sending Body

提问人:Rory L. 提问时间:6/28/2021 最后编辑:Rory L. 更新时间:8/12/2023 访问量:6703

问:

正如标题所说,代码如下。我尝试设置 chunkedTransfer=false、Content-Type application/json、WWWForm、手动构建 JSON 对象和 HttpClient。对于 Content-Type application/json,API 甚至没有命中。其余的,身体是一个空的物体。我不知道这里的问题是什么,通过查看 StackOverflow、YouTube、Unity 文档和所有其他资源。

截至今天早上,我正在使用 Newtonsoft.Json 来序列化 JSON 正文。我认为现在最大的问题是,当我设置 API 路由时,甚至没有收到请求。webRequest.SetRequestHeader("Content-Type", "application/json");

async Task<string> makeRequest()
        {
            string url = API_BASE + "/users"; 
            
            Dictionary<string, string> body = new Dictionary<string, string>();
            body.Add("username", username);
            body.Add("password", password);
            body.Add("email", email);

            using (UnityWebRequest webRequest = UnityWebRequest.Post(url, JsonConvert.SerializeObject(body)))
            {
                await webRequest.SendWebRequest();

                string result = Encoding.UTF8.GetString(webRequest.downloadHandler.data);

                if (webRequest.result != UnityWebRequest.Result.Success)
                {
                    JSONNode error = JSON.Parse(result);

                    registerAndLoginError.GetComponent<Text>().text = error["error"]["message"];
                    registerAndLoginError.SetActive(true);

                    return "";
                }
            }

            BasicLogin();
            return "";
        }
C# unity-游戏引擎 unitywebrequest

评论

0赞 ChocooPanda 1/11/2023
你是怎么导入的?JsonConvert

答:

0赞 derHugo 6/28/2021 #1

内置不支持字典它遵循与 Inspector 相同的序列化规则,请参阅脚本序列化JsonUtility

=> 将返回或在最好的情况下返回。JsonUtility.ToJson(body)"""{}"

尝试“手动”创建JSON字符串,例如

var json = "{\"username\":\""+username+"\", \"password\":\""+password+"\", \"email\":\""+email+"\"}";

或者使用不同的库,例如 (通过 PackageManager 作为 Package 提供),它支持集合和 Dictionary 的直接(反)序列化。Newtonsoft .NET Json

评论

0赞 Rory L. 6/28/2021
感谢您的回复!我实际上也尝试过,我忘了包括它。同样的问题。编辑:更新以包括 JSON 对象手动创建尝试。
12赞 Rory L. 6/28/2021 #2

所以我在其他地方看到了这个解决方案,但我继续忽略它,因为它很笨拙,似乎是应该解决的问题。但是,我现在不在乎。

  1. 用代替UnityWebRequest.PutUnityWebRequest.Post
  2. 设置webRequest.method = "POST";
  3. 设置webRequest.SetRequestHeader("Content-Type", "application/json");

这有效,但感觉真的很糟糕,没有任何意义。

评论

0赞 Maurice Schleußinger 9/3/2022
正是这个。可悲的是,Unity 2021.3.8f1 仍然如此。
0赞 Maurice Schleußinger 9/3/2022
另外,请参阅Unity论坛中有关未来潜在发展的帖子:forum.unity.com/threads/...
0赞 arkantose1 8/12/2023 #3

我使用了上述所有方法,但我仍然被告知我的身体是空白的 {}

我用的有点搞笑

private IEnumerator SendScores(int score)
{

    WWWForm form = new WWWForm();
    form.AddField("score", score);
    form.AddField("module_id", _moduleID);
    form.AddField("user_id", _userCode);

    Debug.Log(form);
    UnityWebRequest www = UnityWebRequest.Post("http://pbs-web-git-sc-14-order-and-payment-system-pbs-labs.vercel.app/api/scores", form);

    yield return www.SendWebRequest();
    if (www.result != UnityWebRequest.Result.Success)
    {
        Debug.Log(www.error);
        Debug.Log("Response: " + www.downloadHandler.text);
    }
    else
    {
        Debug.Log("Form upload complete!");
    }

    QuizManager.Instance.ResetQuizScore();
}

并将其作为表单发送实际上已被 API 接受。

当我后来去做的时候,我再次被告知 API 正在接收一个空的正文。

当我将其发送到模拟 API 时,尽管 JSON 版本和 FORM 版本都在发送时发布格式正确的正文。