0

I need to receive image from a simple server, using wgsi, in my client app in Unity3D. My code looks like this for now:

Server part:

if environ['REQUEST_METHOD'] == 'GET':
        status = '200 OK'
        headers = [('Content-type', 'image/png')]
        start_response(status, headers)

        return open("./static/uploads/04b32b3b6249487fbe042cadc97748b5.png", "rb").read()

Client:

    UnityWebRequest www = UnityWebRequestTexture.GetTexture(uploadURL);

    yield return www.SendWebRequest();

    if (www.isNetworkError || www.isHttpError)
    {
        Debug.Log(www.error);
    }
    else
    {
        Debug.Log("gno");
        this.GetComponent<RawImage>().texture = DownloadHandlerTexture.GetContent(www);
    }
}

My doubt is that UnityWebRequestTexture.GetTexture() always takes an URL that points directly to an image, where in my case it doesn't, but the GET method directly returns an image to the client.

All the POST methods where the client sends images to the server work with no problems, while I get a Failed to receive data error in the Unity Editor.

1 Answers1

0

The problem was simply that i wasn't putting the content length in the headers. So the server part should be:

if environ['REQUEST_METHOD'] == 'GET':
        status = '200 OK'
        headers = [('Content-type', 'image/png')]
        img=open("./static/uploads/"+"imageName", "rb").read()
        start_response(status,[
            ('Content-type', 'image/png'),
            ('Content-Length', str(len(img))),
        ])

        return img