我试图用摄像头捕获的每一帧发送图像字节,使用post请求在NodeJS API中进行处理。
使用此代码:
Debug.Log(imageBytes.Length); //Prints a number
Debug.Log(imageBytes); //Prints the array type (?)
Debug.Log(imageBytes[1]); //Prints the byte
UnityWebRequest www = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
UploadHandlerRaw handler = new UploadHandlerRaw(imageBytes);
handler.contentType= "application/x-www-form-urlencoded";
www.uploadHandler = handler;
Debug.Log(www.uploadHandler.data[1]);
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
string jsonResponse = www.downloadHandler.text;
Debug.Log(jsonResponse);但是,当我在API中执行console.log时,它会打印req.body。
{}
。显然,这些数据并不是由UnityWebRequest发送的。
对此有什么想法吗?
发布于 2018-06-25 16:25:36
我找到了一种办法让这件事奏效。
需要将统一中的代码更改为:
using (UnityWebRequest www = UnityWebRequest.Post(url, webForm))
{
www.SetRequestHeader("Content-Type", "text/html");
www.uploadHandler = new UploadHandlerRaw(imageBytes);
www.uploadHandler.contentType = "text/html";
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
}在Node.JS中:
app.use(cors());
var options = {
inflate: true,
limit: '3000kb',
type: 'text/html'
};
app.use(bodyParser.raw(options));发布于 2018-06-20 08:17:11
您对二进制数据使用了错误的内容类型。如果使用WWWForm类,它将自动将此头设置为多部分/表单数据。试试下面的代码:
var wwwForm = new WWWForm();
wwwForm.AddBinaryData ("image", imageBytes, "imagedata.raw");
var request = UnityWebRequest.Post (url, wwwForm);
yield return request.SendWebRequest ();发布于 2018-06-19 22:43:14
几个月前,我尝试了一些新的www请求类,我在将数据发布到服务器时遇到了类似的问题,然后我又回到了旧的www类来处理它,它工作起来很有魅力。
https://stackoverflow.com/questions/50931630
复制相似问题