Search Unity

Using WWW class in WebGL to send POST data with custom headers

Discussion in 'Editor & General Support' started by ian_umemoto, Mar 24, 2017.

  1. ian_umemoto

    ian_umemoto

    Joined:
    Mar 4, 2016
    Posts:
    1
    Edit: I appear to have figured out how to post the data, but the content-type is x-www-form-urlencoded and the server only supports application/json. Is there a way to do this with Unity and WebGL?


    I'm trying to use a RESTFUL API and send POST data to our server. Here is what I am trying and the POST data is appearing empty on the server side.

    Here is the cURL request I am trying to make
    curl -X POST \
    -H "CUSTOM HEADER: MyHeader" \
    -H "CUSTOM HEADER2: MyHeader2" \
    -H "Content-Type: application/json" \
    -d '{"unitykeytest":"unitykeyvalue"}' \
    https://mywebserver/my/restful/api


    [Attempting: POST request - custom headers - POST data]

    var header = new Dictionary<string, string>();

    header.Add("CUSTOM HEADER", MyHeader1);
    header.Add("CUSTOM HEADER2", MyHeader2);
    header.Add("Content-Type", "application/json");

    string json = "{ \"unitykeytest\": \"unitykeyvalue\"}";
    string url = "http://mywebserver/test/requestTest.php";
    WWW www;
    var encoding = new System.Text.ASCIIEncoding();
    www = new WWW(url, encoding.GetBytes(json), header);

    yield return www;

    // Handle www request

    On the server side, I am getting an empty $_POST variable
    Array
    (
    )

    with everything else looking correct.
    I have tried RESTSharp and other libraries for c# but they all use Async and will not work on WebGL.

    Thank you
     
    Last edited: Mar 24, 2017