Sakuna Madushanka
Sakuna Madushanka

Reputation: 178

Unity WebGL POST requests does not work after build the game

my unity webGL Game doesn't seems sending post request to the server when after build the game, but its working fine in the editor. Is there anything to do additionally than this?

IEnumerator PostRequestY(string url, string jsonToAPI)

{
    Debug.Log("--------------Game Data Sending --------------");
    var uwr = new UnityWebRequest(url, "POST");
    jsonToAPI = jsonToAPI.Replace("'", "\"");
    //Debug.Log(jsonToAPI);
    byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(jsonToAPI);
    uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
    uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
    uwr.SetRequestHeader("Content-Type", CONTENT_TYPE);
    uwr.SetRequestHeader("x-api-key", X_API_KEY);

    //Send the request then wait here until it returns
    yield return uwr.SendWebRequest();

    if (uwr.isNetworkError)
    {
        Debug.Log("Error While Sending Game Data: " + uwr.error);
        Debug.Log("URL ::: " + url);
        messageText.text = uwr.error;
    }
    else
    {
        //Debug.Log("Game Data Received: " + uwr.downloadHandler.text);
        string recievedMessage = uwr.downloadHandler.text;
        //Debug.Log("Respond for Game Data " + recievedMessage);
        messageText.text = uwr.downloadHandler.text +" & "+ accessToken;
    }
}

This message appears when the game is played in the editor...

This message appears when the game is played in the editor...

This message appears when the game is played after the build... enter image description here

Why this is happens? Any suggestions would be highly appreciated... Thanks

Upvotes: 2

Views: 2670

Answers (2)

Codemaker2015
Codemaker2015

Reputation: 15700

For the server to accept my request, You should add a header to the API response itself, with the header and its value being Access-Control-Allow-Origin: domainName or Access-Control-Allow-Origin: *

For example:

public class BookApi: MonoBehaviour {
    private const string URL = "https://retrofit-backend-demo.herokuapp.com/book";

    public void GenerateRequest () {
        StartCoroutine (ProcessRequest (URL));
    }

    private IEnumerator ProcessRequest (string uri) {
        using (UnityWebRequest request = UnityWebRequest.Get (uri)) {
            request.SetRequestHeader("Access-Control-Allow-Origin", "*");
            yield return request.SendWebRequest ();

            if (request.isNetworkError) {
                Debug.Log (request.error);
            } else {
                JSONNode books = JSON.Parse(request.downloadHandler.text);
                Debug.Log(books["facts"][0]);
            }
        }
    }
}

Upvotes: 1

DoctorPangloss
DoctorPangloss

Reputation: 3073

In Unity WebGL, the underlying web request is done with fetch or XMLHttpRequest, which means you must correctly configure CORS on your server. You are not allowed to set nearly any request header by default.

See https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader for more information.

Upvotes: 0

Related Questions