Unity POST张贴空请求

时间:2018-10-12 03:07:55

标签: c# express post

我目前正在为VR游戏Beat Saber编写一个mod,用于重播播放器的性能并将其上传到Express Node.JS服务器进行存储。但是,无论何时上传重播(另存为JSON),我的服务器都会将req.body作为空对象返回,并且req.file是未定义的。

我正在使用bodyparser解析JSON和multer解析Express代码中的文件。

我尝试过:

  • WWWForm
  • System.Net.Http(通过NuGet)
  • 哈希表和字典以填写WWWForm(而不是form.AddField()
  • UnityWebRequest

使用Postman来测试我的服务器代码,服务器可以很好地处理Postman的请求,所以我得出的结论是这是客户端问题。

C#上传代码:

            List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
            formData.Add(new MultipartFormDataSection(string.Format("songName={0}&songSubName={1}&authorName={2}&difficulty={3}", songName, songSubName, authorName, difficulty)));
            formData.Add(new MultipartFormFileSection("replayData", Encoding.UTF8.GetBytes(builder.ToString() /*Compressed JSON*/), "file", "application/json"));
            UnityWebRequest w = UnityWebRequest.Post("http://caed.jackz.me/ghostmode/UploadReplay", formData);
            w.SetRequestHeader("Content-Type", "application/json; charset=utf8");
            w.chunkedTransfer = false; //Some of this code was trial and error through others question
            UnityWebRequestAsyncOperation request = w.SendWebRequest();
            request.completed += uploadComplete;

快递代码:

app.post("/ghostmode/UploadReplay", upload.array(), (req,res) => {
  console.log(moment.tz(moment(), "America/Los_Angeles").format("h:mm:ss A"));
  console.log(req.body); //bodyparser.json()
  console.log(req.files); //multer
});

服务器的控制台输出始终为

7:54:07 PM //Timestamp so I know when its from
{} //req.body via bodyparser
undefined //req.files via multer

0 个答案:

没有答案