0

I'm trying to use Unity 2017.3 to send a basic HTTP POST request from Unity scripting. I want to send an image data, which I can access in my script either as file.png or in a byte[]. I am only posting to a local server running Python/Django, and the server does register the POST request coming in - but no matter what I do the request arrives empty of any content, body, files or raw data at my web app.

IEnumerator WriteAndSendPng() {
    #extra code that gets bytes from a render texture goes here
    #can verify that drawing.png is a valid image for my purposes

    System.IO.File.WriteAllBytes("drawing.png", bytes);
    List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    formData.Add( new MultipartFormFileSection ("drawing", bytes, "drawing.png", "image/png") );
    UnityWebRequest www = UnityWebRequest.Post("http://127.0.0.1:8000/predict/", formData);
    yield return www.SendWebRequest();
    if(www.isNetworkError || www.isHttpError) Debug.Log(www.error);
    else Debug.Log("Form upload complete!");
}

I'm following the docs and there are a bunch of constructors for MultipartFormFileSection, most of which I feel like I've tried. Also tried UploadHandlers, or the old AddBinaryField WWW API - still the request is always empty when it hits my app... I've read the thorough response to this SO ticket, Sending http requests in C# with Unity. I have tried many of the implementations here but again, Django receives empty requests. Even submitting the simplest possible request from Unity sends empty requests. So weird.

    List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    formData.Add (new MultipartFormDataSection ("someVar=something"));

The Python server just sees:

[11/Feb/2018 14:14:12] "POST /predict/ HTTP/1.1" 200 1
>>> print(request.method) # POST
>>> print(request.encoding) # None
>>> print(request.content_type) #multipart/form-data
>>> print(request.POST) # <QueryDict: {}>
>>> print(request.body) # b''

I thought it might be a Django issue, but posting to the same app w/ Postman or from other sources, it sees the incoming data just fine. Anyone done this recently? I thought this would be a piece of cake and many hours into into it I remain stymied. All help appreciated! Thanks, all.

jckstl
  • 314
  • 3
  • 11

1 Answers1

0

Figured it out courtesy of Unity staffer Aurimas-Cernius on their forums: "The issue most likely is that your Django server does not support HTTP 1.1 chunked transfer. You can either try updating your server, or set chunkedTransfer property on UnityWebRequest to false."

He was right. Setting that flag to false allowed me to start sending simple test case data and receiving it as expected in the Django app - bet I'll be able to get images working in no time. I was also experiencing side effects of using Python 3.5.x (mistakenly assuming I needed to). Upgrading that fixed the chunk issue, too. Cheers!

jckstl
  • 314
  • 3
  • 11