admin管理员组文章数量:1296889
I am trying to send a PUT request using UnityWebRequest to update game data on a server. However, I keep getting the error:
css Copy Edit Error: 400 - {"message":"Invalid json data"} I have tested the same JSON in Postman, and it works correctly. But when sending it from Unity, the request fails with a 400 Bad Request error.
API's request sample
'{
"gameId": "93bdd165-51aa-4aab-87c2-516a234465b9",
"used": {
"startBoosts": [
{
"name": "Head start",
"quantity": 8
},
{
"name": "Score multiplier",
"quantity": 4
}
],
"normalBoosts": [
{
"name": "Magnet",
"quantity": 8
},
{
"name": "Force Shield",
"quantity": 6
},
{
"name": "Hyperdrive",
"quantity": 1
}
]
},
"collected": [
{
"name": "Magnet",
"quantity": 1
},
{
"name": "Force Shield",
"quantity": 0
},
{
"name": "Hyperdrive",
"quantity": 3
}
],
"finalScore": 36598521
}'
json (string) generated in unity before uploading within the PUT method request
Generated JSON: {
"gameId": "8eb97e75-8127-47e9-abd5-895e094b4e32",
"used": {
"startBoosts": [
{
"name": "Head start",
"quantity": 0
},
{
"name": "Score multiplier",
"quantity": 0
}
],
"normalBoosts": [
{
"name": "Magnet",
"quantity": 0
},
{
"name": "Force Shield",
"quantity": 0
},
{
"name": "Hyperdrive",
"quantity": 0
}
]
},
"collected": [
{
"name": "Magnet",
"quantity": 0
},
{
"name": "Force Shield",
"quantity": 0
},
{
"name": "Hyperdrive",
"quantity": 0
}
],
"finalScore": 0
}
Code:
private IEnumerator SendDeclareGameEndRequest()
{
// Ensure BoostsManager is set
BoostsManager boostsManager = FindObjectOfType<BoostsManager>();
if (boostsManager == null)
{
Debug.LogError("BoostsManager not found!");
yield break;
}
// Get game data
GameEndData gameEndData = boostsManager.GetGameEndData(gameId, score.score);
// Convert to JSON using Newtonsoft.Json
string json = JsonConvert.SerializeObject(gameEndData, Formatting.Indented);
Debug.Log("Generated JSON: " + json); // Debugging output
byte[] jsonToSend = System.Text.Encoding.UTF8.GetBytes(json);
using (UnityWebRequest request = new UnityWebRequest(apiUrl, "PUT"))
{
request.uploadHandler = new UploadHandlerRaw(jsonToSend);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("session-token", sessionToken);
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
Debug.Log("Game End Declared Successfully: " + request.downloadHandler.text);
}
else
{
Debug.LogError("Error: " + request.responseCode + " - " + request.downloadHandler.text);
}
}
}
Debugging Steps Taken
- Checked the generated JSON in Unity logs and verified it matches Postman.
- Ensured Content-Type is correctly set to application/json.
- Tried sending data as a string instead of a byte[], but UnityWebRequest requires byte[] for PUT.
- Used both Unity’s built-in JsonUtility and Newtonsoft.Json for serialization.
本文标签: Error 400quotmessagequotquotInvalid json dataquot While using Post method in UnityStack Overflow
版权声明:本文标题:Error: 400 - {"message":"Invalid json data"} While using Post method in Unity - Stack Overfl 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741638663a2389785.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论