- Home /
WWWForm.Addfield not able to post data to server
I need to post data to a server. The server connection is made . But data is not posted in the server. I have correct script in curl/php. But its equivalent WWWform.Addfield is not working properly.
php script:
$oauth_consumer_key = 'mvKm39WrNkp4enY2Ufk5nnJ5qqoLoPkC';
$oauth_token = 'BYUTPDgspPDHiGcppXRtRqC7btBbKqGS';
session_start();
global $user;
$username = trim("visvanathan-a");
$password = trim(base64_encode("------@3620"));
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, 'http://10.98.12.54/sandbox/services/json?oauth_consumer_key='.$oauth_consumer_key.'&oauth_token='.$oauth_token.'');
$data = 'method="user.login"&username="'.$username.'"&password="'.$password.'"&domain="HCLTECH"';
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
$res = json_decode($result,true);
echo "Test Call ---- ";
print_r($res);
My equivalent unity Script
#pragma strict
import System.IO;
import System.Text;
import System;
private var str : String ="";
private var url = "http://10.98.12.54/sandbox/services/json?oauth_consumer_key=GGCbkzxLMCtwtCFjXPrjpdu98Xnj7QUy&oauth_token=xMZZMByTY5aL94ARqcASqEp4o7QEvKSz";
function Start ()
{
var form= new WWWForm();
form.AddField("method","user.login"+);
form.AddField("username","vigneshwari.ramu");
form.AddField("password","sasffgfhhhhj");
form.AddField("domain","HCLTECH");
var hs_post = WWW(url,form.data);
yield hs_post;
if(hs_post.error)
{
print("There was an error posting the high score: " + hs_post.error);
}
str = hs_post.text;
print(str);
}
Please say what problem in my unity code. why iam not able post username, password and domain
Answer by XienDev · Mar 21, 2013 at 06:50 AM
before post add this code:
var headers = form.headers;
if (!headers.Contains("Content-Type"))
{
headers.Add("Content-Type", "application/x-www-form-urlencoded");
}
var hs_post = new WWW(url, form.data, headers);
That solved my problems =)
2) have you added crossdomain.xml ?
@XienDev I tried what you said . Still its not working In the server log
$$anonymous$$ethod = userlogin -> this got posted But remaining
username = "" password = "" domain = ""
is not getting posted . Its showin nothing in the server log for these fields.
I get output as
{ "#error": true, "#data": "Invalid method " }
But I should be getting output as
[#error] => [#data] => stdClass Object ( [sessionid] => gnCgd3$$anonymous$$bwbqBe96_k$$anonymous$$2R15_Ar5-umb$$anonymous$$lqZc [user_id] => 25452 [user_first_name] => Visvanathan [user_last_name] => Arumugam [photo_url] => http://localhost/$$anonymous$$E$$anonymous$$E/sites/default/files/pictures/picture-25452-142550.jpg )
When my metho field gets posted why not my username, poassword, domain field not posted in server?
XienDev
No I have not added crossdomain.xml
Iam trying to post data to server and get a response Iam new to this. The php script given above works fine in posting and retrieving response from sever But my equivalent unity script shows error . Connection made but post unsuceessful to server
No I have not added crossdomain.xml
Iam trying to post data to server and get a response Iam new to this. The php script given above works fine in posting and retrieving response from sever But my equivalent unity script shows error . Connection made but post unsuceessful to server
content type and application/x-www-form-urlencoded refers to what? Sorry Iam newbie at this
Your answer
Follow this Question
Related Questions
Retrieving varibale values from a php script? 1 Answer
WWW Class is acting up 1 Answer
Why isn't this $_POST working properly? 1 Answer
wwwform send an array the right way! 0 Answers
Would a WWW to a PHP file return the computers Info? 0 Answers