0

Hi every one, I have a login form which has mail id and password, on submit it should call an api which inturn generates a auth token. I have used POST method in Php curl but getting the following error.

Apache Tomcat/6.0.36 - Error report .. some html styles.. The server refused this request because the request entity is in a format not supported by the requested resource for the requested method

and this is my ajax call..

function callapi()
{ 
data = new Object();
var email= document.getElementById("input01").value;
var pwd=document.getElementById("input02").value;
if(email != ""&& pwd != "")
{
data.Email = email;
data.Pwd = pwd;

}

jQuery.ajax({ 
  url: "http://localhost/stackato/nlogin.php",
  type: "POST",
  dataType:"json",
  data:data,
  error: function (data) {
   alert('error--'+data);
  },
  success: function (data) { 

if(data=="invalid")
{

}
else
{

window.location.href="finalstackatolists1.html";
}



}
});

}

and this is my curl php page

<?php
header('Content-type: application/json');
$url = $_SERVER['REQUEST_URI'];
$scriptname = $_SERVER['SCRIPT_NAME']; 
$queryString = $_SERVER['QUERY_STRING'];
$mail=$_POST["Email"];
$pwd=$_POST["Pwd"];
$action = $_POST["action"];


$nurl = str_replace($scriptname,"...the api...",$url);




//open connection

$ch = curl_init(); 
$data = array("username" => $mail, "password" => $pwd);                                                                    
$req_data = json_encode($data);  

curl_setopt($ch, CURLOPT_URL, $nurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req_data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);


 //execute post

if( ! $result = curl_exec($ch)) {
    die(curl_error($ch));
    curl_close($ch);
}

if(curl_getinfo($ch, CURLINFO_HTTP_CODE) == '500')
{
    header("Status: 500 Server Error");
}

curl_close($ch);


echo $result;


?>

(I tried it in REST-Client and the token has been generating)

Please help me to solve this.. thanks in advance..

10
  • I suspect header('Content-type: application/json'); should be after curl_close() Commented Dec 26, 2012 at 11:20
  • @Dr.Dan: Tried but still getting the same error. Commented Dec 26, 2012 at 11:27
  • in your curl add curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/json")) also refer this Commented Dec 26, 2012 at 11:37
  • @mithunsatheesh: Tried it but getting the similar kind of error report but now its coming as "The request sent by the client was syntactically incorrect" Commented Dec 26, 2012 at 12:15
  • @vignesh: comment the curl code and try to output the $req_data.Check the format is correct. Commented Dec 26, 2012 at 12:31

1 Answer 1

0

You have to specify that the posted data is of type JSON

add this line to your curl code

curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/json"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.