问题描述
我想从我的Unity应用程序向我的Laravel后端发送一个JSON文件.我的发帖请求似乎出了点问题,但我不知道这是什么.
I want to send a JSON file from my Unity application to my Laravel backend. It seems there is something wrong with my post request but I can't figure out what it is.
public void SendRequest()
{
// serializable struct
OfferData data = new OfferData(OfferSize.JUMBO, OfferType.AD, 50, 10);
StartCoroutine(RequestHandler.Post(API_URL + "shop/store", JsonUtility.ToJson(data), (response) =>
{
if (response == null)
{
return;
}
// success
Debug.Log(response);
}));
}
JSON:
{
"size":3,
"type":1,
"gold":50,
"gems":10
}
RequestHandler中的post函数:
The post function in the RequestHandler:
public static IEnumerator Post(string uri, string data, Action<string> response)
{
UnityWebRequest webRequest = UnityWebRequest.Post(uri, data);
webRequest.SetRequestHeader("Content-Type", "application/json");
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError)
{
Error(webRequest.error);
response(null);
}
else
{
response(webRequest.downloadHandler.text);
}
}
如果我与Laravel发送 UnityWebRequest ,则会发送成功响应,但是,打印数组(见下文)只是空的. Laravel无法对JSON进行编码.
If I'm sending the UnityWebRequest with Laravel will send a success response, however, the printed array (see below) is just empty. Laravel can not encode the JSON.
这是在我的控制器功能中访问HTTP Post请求的位置:
This is where the HTTP Post request is being accessed in my controller function:
public function store(Request $request)
{
Log::debug($request->json()->all());
}
使用邮递员会产生哪些预期结果:
Which produces the expected result when using Postman:
array ('size' => 3, 'type' => 1, 'gold' => 50, 'gems' => 10)
使用Unity使用相同的JSON发送类似的请求:
Sending a similar request with the same JSON using Unity:
array ()
当我使用$request->getContent()
时,我实际上可以看到我的数据.为什么数组为空?
When I use $request->getContent()
I can actually see my data. Why is the array empty?
从邮递员发送时的请求标头:
The request header when sent from Postman:
{
"content-type": "application/json",
"user-agent": "PostmanRuntime/7.21.0",
"accept": "*/*",
"cache-control": "no-cache",
"postman-token": "d8f323fc-f2c8-49b8-a023-2955122fa20e",
"host": "127.0.0.1:8000",
"accept-encoding": "gzip, deflate",
"content-length": "119",
"cookie": "XSRF-TOKEN=eyJpdiI6IkRyU1RqSkppcVdyUmpuVHI2Ym55XC9RPT0iLCJ2YWx1ZSI6ImQrc1QrOTcyWE1GSXM3SGQrVlBsTG1lZ0dXd1FCQlRPellTQm83Z0FkWFc0UktjUW9PNHRqS3B3Z2Rya1ZZS2IiLCJtYWMiOiIxMmNlZTFiODc2MTlmNmVhYjI3ZGI1MTQ1NTM2MGFjODQ4YjZhNzdlMmM4NWQwM2NiYzk1MjkzYzNiYjBmNTA5In0%3D; recludo_session=eyJpdiI6ImFLcUdCdU1WU2JzazNEaEFyaGoxbnc9PSIsInZhbHVlIjoiT1VtNzl4XC9HMW5reTdKUGNDdlBXMVdLK3hMNFR3Q2JxMzA1RVY3NWdVdmV5akJhbnBKeU9YdU5JSmdPdGYyNWUiLCJtYWMiOiI4MWJjOGVhMTcxNDI3M2VjNTU0MDc3NmNkZDU0NjZlMzhmYWI1MjRlZGNlZjhhNGEyNmNjMmY3YThiMzAyODNhIn0%3D",
"connection": "keep-alive"
}
错误的请求标头
从Unity发送时的请求标头:
Faulty request header
The request header when sent from Unity:
{
"host": "127.0.0.1:8000",
"user-agent": "UnityPlayer/2019.2.8f1 (UnityWebRequest/1.0, libcurl/7.52.0-DEV)",
"accept": "*/*",
"accept-encoding": "deflate, gzip",
"cookie": "XSRF-TOKEN=eyJpdiI6IlJhZE52emU1Z3hYUnVOWmtMbEdZa0E9PSIsInZhbHVlIjoibkdabkhZVnM1ZUYwSklvMzYrSHVLQ0Q5Y2NvRlVTMEhJOHpqMGFCSEZLZVQwd2NnT3NrUmNrXC9cL2Z4XC92M0J0QSIsIm1hYyI6Ijg4ZDUwZDQ4MWQ3OWM3ZjNlOTcxOWE3NzMxYjI1MmQ3NGQ2YzgwMWQ2MDE2YTQ5NTI3NWQ3MTg2ODM4NjMxY2UifQ%3D%3D; recludo_session=eyJpdiI6InoyMktDN3ByR1hYR0tHWCtvdmhOckE9PSIsInZhbHVlIjoiamZJVnlDbVYwZkdBU1wvMXhMeG1sWU5LdDY0d0NnQ0VucE1OK05UNDhUOG1Ya2o5ZUJIcFdaSktuakcrQjJqN1QiLCJtYWMiOiI1YTc5YTE5NDNhNjY5NWRlYzlmMDlkOGIyMWRiYTAzYzMwZTkwNzAzYjBhNTA2OGViOTUyOTlkYzMzYWJlMjA3In0%3D",
"content-type": "application/json",
"x-unity-version": "2019.2.8f1",
"content-length": "215"
}
我想念什么?请让我知道是否需要更多信息.
What am I missing? Please let me know if you need any more information.
推荐答案
我仍然不确定为什么它不能立即使用,但我想出了解决该问题的方法.
I'm still not sure why it doesn't work out of the box but I figured out how to fix this issue.
将原始上传处理程序添加到 UnityWebRequest :
Add a raw upload handler to the UnityWebRequest:
UnityWebRequest webRequest = UnityWebRequest.Post(uri, data);
// Fix: Add upload handler and pass json as bytes array
webRequest.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(data));
webRequest.SetRequestHeader("Content-Type", "application/json");
yield return webRequest.SendWebRequest();
Laravel现在可以正确解析JSON数据.
The JSON data can now be resolved correctly by Laravel.
这篇关于如何获取HTTP Post请求的JSON内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!