- Home /
Sending Unity Web Request Post returning empty from webhost
iv been stuck on this for a long time and I just can't seem to figure out what the problem is. This is working perfectly if im connecting to http://localhost/ but if I try it on my domain and host online then it suddenly doesn't return any information.
This is my httpPost class:
public static IEnumerator httpPost(string path, string data, Action<APIHttpRequestModel> callback)
{
byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes(data);
var www = new UnityWebRequest(path, UnityWebRequest.kHttpVerbPOST);
www.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
Debug.Log(www.uploadHandler.data); // This still returns the JSON string it's sending
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.Log("Something went wrong, and returned error: " + www.error);
callback(new APIHttpRequestModel(false, 400, www.error));
} else
{
Debug.Log(www.downloadHandler.text); // This returns empty because server isn't receiving any data
if (www.responseCode == 200)
{
callback(new APIHttpRequestModel(true, www.responseCode, www.downloadHandler.text));
Debug.Log(www.downloadHandler.text);
}
else
{
callback(new APIHttpRequestModel(false, www.responseCode, www.downloadHandler.text));
Debug.Log(www.downloadHandler.text);
}
}
}
I've tried debugging and these is what iv found so far:
- If I try to Debug.Log() the www.uploadHandler.data it is populated with data.
If I output the request data on the server there is no data getting returned
If I try to connect to the same link, on the web host, through a REST Client, it's returning the proper data
It definitely is hitting the URL and path specified, but when I try to return the request data, it's just empty.
TL;DR: The code is hitting the endpoint, but not returning data (trying to output the data it sent) on a production server, if I do it locally it's working fine. If I try with a REST client to production server it's working fine aswell.
EDIT 1: I downloaded the latest version of unity and it seems to be working fine there, so it's something related to 2017.3.1f1
Hello, I guess it might be related with your production server. so may I suggest checking your hosted server's firewall, I am not pro, but lately I was fiddling with web server and checking firewall daily, there is lot of malicious requests has to be filtered away from legit requests. Sorry if this is not related at all.
The thing is it's working fine when i try with a RESTfull client like Postman, it's doing everything correctly there so it can't be a server related thing otherwise it would block the restfull client's request aswell.
Answer by mikelmao · Jul 26, 2018 at 02:00 PM
Apperantly this is a bug in some versions of Unity (I'm using version 2017.3.1f1)
The reason this is happening is because unity is sending the following header along with the request: "Transfer-Encoding: chunked" which many server's can't handle.
Adding www.chunkedTransfer = false;
fixes this and will set it to the header to Content-Length