Search Unity

WWW / WWWForm to post json data

Discussion in 'Scripting' started by ozkan13700, Sep 16, 2018.

  1. ozkan13700

    ozkan13700

    Joined:
    Dec 30, 2017
    Posts:
    7
    Hello firstly sorry for my bad english , I need help to make my own authentification system for roblox I use roblox auth api : https://auth.roblox.com/docs#!/Authentication/post_v2_login , but I cannot get a valid response from the server. I get an error code 400 "Username and Password are required. Please try again."

    I tested following curl command structure it work fine :

    Code (CSharp):
    1. curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
    2.   "ctype": "Username", \
    3.   "cvalue": "INeoxz13700", \
    4.   "password": "ddddd" \
    5. }' 'https://auth.roblox.com/v2/login'
    my c# scripts :

    Code (CSharp):
    1. IEnumerator request()
    2.     {
    3.         var content = "{ \"ctype\" : \"Username\" , \"cvalue\": \"INeoxz13700\", \"password\" : \"ddddd\"}";
    4.  
    5.         var encoding = new System.Text.UTF8Encoding();
    6.  
    7.         var headers = new Hashtable ();
    8.  
    9.         headers.Add("Content-Type", "application/json");
    10.         headers.Add("Accept", "application/json");
    11.         headers.Add ("X-CSRF-TOKEN", "sendfalsetokken");
    12.  
    13.         //Send a request with a bad tokken to get a valid token from roblox system
    14.         var request = new WWW ("https://auth.roblox.com/v2/login", encoding.GetBytes (content), headers);
    15.  
    16.         yield return request;
    17.  
    18.         //I get a error 403 Token Validation Failed it's logic the token in headers is bad
    19.         Debug.Log (request.text);
    20.  
    21.         //this foreach allow me to recover the token from roblox
    22.         foreach(KeyValuePair<string, string> entry in request.responseHeaders)
    23.         {
    24.             if (entry.Key == "X-CSRF-TOKEN") {
    25.                 Tokken = entry.Value;
    26.             }
    27.         }
    28.  
    29.         //And finally I resend a new post request with a correct token
    30.         headers = new Hashtable();
    31.  
    32.         headers.Add("Content-Type", "application/json");
    33.         headers.Add("Accept", "application/json");
    34.         headers.Add ("X-CSRF-TOKEN", Tokken);
    35.  
    36.  
    37.         request = new WWW ("https://auth.roblox.com/v2/login", encoding.GetBytes (content), headers);
    38.  
    39.         yield return request;
    40.  
    41.         //And I get a error 400 Username and Password are required. Please try again. I don't know why ...
    42.         Debug.Log (request.text);
    43.  
    44.  
    45.         yield break;
    46.     }
    thanks for your helps .
     
    Last edited: Sep 16, 2018
  2. ozkan13700

    ozkan13700

    Joined:
    Dec 30, 2017
    Posts:
    7
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    I don't see you using token in your curl request.
    Best install a HTTP proxy for debugging this (like Fiddler) and compare your curl request with what Unity sends.
     
  4. ozkan13700

    ozkan13700

    Joined:
    Dec 30, 2017
    Posts:
    7
    thanks for your answer I finally tested to send Json post request with Java and it's work greatly I get a response code 200 ,do you have any idea ?
    why it's not work with Unity web request ??

     
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    Certainly some silly mistake. Have you tried installing Fiddler or a similar tool? Answer to this lies in raw requests being sent.