- Home /
Set Content-Length header for UnityWebRequest POST requests in 2017.3?
I am trying to send multipart form data via a UnityWebRequest
to a REST service. The request is built with the following code:
public IEnumerator Post(string uri, WWWForm data, Action<UnityWebRequest, Exception> onFinished = null)
{
Request = UnityWebRequest.Post(uri, data);
yield return sendRequest();
if (onFinished != null)
onFinished(Request, UploadException);
}
(This is missing some context from the rest of the class, but the important part is the UniytWebRequest.Post(uri, data);
line).
In previous versions of Unity this has worked fine, but in 2017.3, the remote server has started returning a 411 Length Required error.
Looking at the request object before it is sent, I see that the Content-Type header is set, but no Content-Length header is present. I attempted to set the header manually, but got the following error: InvalidOperationException: Cannot override system-specified headers
Is there another way to set the outgoing content length header in 2017.3?
Answer by nratcliff · Jan 04, 2018 at 05:24 PM
Ah, got it! Setting Request.chunkedTransfer = false
seemed to do the trick.
Curious that the default state hasn't changed from earlier versions, but now it seems to strip the Content-Length header.
Just ran into the same problem also using 2017.3, and the solution worked for me as well. Thank you!
I also ran into the same issue, but oddly, looking at the legacy documentation for that property, it seems to have been set to true by default for many versions...
Got empty POST requests on my server with 2017.3 version. Your solution works. Thank you sooo much!
Your answer
Follow this Question
Related Questions
Setting up a web service in Unity? 0 Answers
JSON WebRequest works in 2018.2, throws 403 error in 2018.3 0 Answers
Pass Header Data in UnityWebRequest 2 Answers
RESTful libraries for Unity 6 Answers