Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problems uploading screenshot via UnityWebRequest

Discussion in 'Scripting' started by bigbrainz, Apr 18, 2019.

  1. bigbrainz

    bigbrainz

    Joined:
    Jul 21, 2015
    Posts:
    177
    I've been able to find a few other threads similar to this, but none seem able to fix my particular issue. We are uploading a screenshot to our site using the method outlined in the docs. It works in most ways when we use our free server (doesn't work with WebGL), but to avoid cross-domain errors, we need to move it to our own paid server.

    On our paid server it generates a 500 error because mod_security is rejecting it: "Multipart request body failed strict validation: PE 0, BQ 1, BW 0, DB 1, DA 0, HF 0, LF 0, SM 0, IQ 0, IP 0, IH 0, FL 0"]

    The parts its failing there are BQ %{MULTIPART_BOUNDARY_QUOTED} and DB %{MULTIPART_DATA_BEFORE}

    In this thread I found a possible clue, but no explanation about how to work around it:
    Code (CSharp):
    1.     IEnumerator Upload()
    2.     {
    3.         // We should only read the screen buffer after rendering is complete
    4.         yield return new WaitForEndOfFrame();
    5.  
    6.         // Create a texture the size of the screen, RGB24 format
    7.         int width = Screen.width;
    8.         int height = Screen.height;
    9.         Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
    10.  
    11.         // Read screen contents into the texture
    12.         tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    13.         tex.Apply();
    14.  
    15.         // Encode texture into PNG
    16.         byte[] bytes = tex.EncodeToPNG();
    17.         Object.Destroy(tex);
    18.  
    19.         WWWForm form = new WWWForm();
    20.         form.AddBinaryData("file", bytes, "screenShot.png", "image/png");
    21.         UnityWebRequest www = UnityWebRequest.Post("https://ourwebsite.com/process.php", form);
    22.         www.chunkedTransfer = false;
    23.         yield return www.SendWebRequest();
    24.  
    25.         if (www.isNetworkError || www.isHttpError)
    26.         {
    27.             if (www.isNetworkError)
    28.             {
    29.                 Debug.Log("Network Error: " + www.error);
    30.             }
    31.             if (www.isHttpError)
    32.             {
    33.                 Debug.Log("Http Error: " + www.error);
    34.             }
    35.         }
    36.         else
    37.         {
    38.             Debug.Log("Report successfuly sent: " + www.downloadHandler.text);
    39.         }
    40.     }
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Which Unity version (exactly) are you using? The was a bug in the past where we didn't put the boundaries correctly.
    Try running Fiddler or similar and examine the raw request data.
     
  3. bigbrainz

    bigbrainz

    Joined:
    Jul 21, 2015
    Posts:
    177
    We're running 2018.3.11f1.

    In the end we've worked around it by adjusting our server to accommodate the unexpected data format.
     
    Last edited: Apr 18, 2019