dongxingguo1978 2014-11-22 16:15
浏览 32

无法测试Rest api

i am developing a REST API for my android application. At the moment i haven't incoropated it into my application instead i am trying to test it with the popular "Advanced Rest Client App" on chrome. The function handling the GET method works fine and displays records from my db in json format but when i try to test the function handling POST it doesn't work. Please i would like some one to help me out on how to test my post function from "Advanced Rest Client App".

API Code:

<?php
  require_once '../include/DbConnect.php';
  require '.././libs/Slim/Slim.php';

  \Slim\Slim::registerAutoloader();
  // create new Slim instance
  $app = new \Slim\Slim();

  $app->get('/users', 'getUser');
  $app->post('/register', 'addUser');



  $app->run();

  function getUser(){
  $sql = "SELECT * FROM users";
  try{
    $db = new DbConnect();
    $conn = $db->connect();
    $stmt = $conn->query($sql);
    $users = $stmt->fetchAll(PDO::FETCH_OBJ);
    $db = null;
    echo '{"user": ', json_encode($users).'}';

  } catch(PDOException $e){
    echo '{"error":{"text":'. $e->getMessage().'}}';
  }

}

function addUser() {
$request = \Slim\Slim::getInstance()->request();
$body = $request->getBody();
$user = json_decode($body);
$sql = "INSERT INTO users (fname, email) VALUES(:fname, :email)";

try{
    $db = new DbConnect();
    $conn = $db->connect();
    $stmt = $conn->prepare($sql);
    $stmt->bindParam("fname", $user->fname);
    $stmt->bindParam("email", $user->email);
    $stmt->execute();
    $user->id = $conn->lastInsertId();
    $db = null;

   echo json_encode($user);

} catch(PDOException $e){
    echo '{"error":{"text":'. $e->getMessage().'}}';
}
}
?>
  • 写回答

1条回答 默认 最新

  • doutang3815 2014-11-22 17:08
    关注

    Okay, here is my issue, when i echo the content of the $body in the code below these values are displayed "fname=mishael&email=mishaelharry%40gmail.com" but after i decode it with $user = json_decode($body); no values are inserted into my database and i get {"fname":null,"email":null,"id":"33"} from this line of code echo json_encode($user); [code]

    function addUser() {
    $request = \Slim\Slim::getInstance()->request();
    $body = $request->getBody();
    echo json_encode($body);
    $user = json_decode($body);
    $sql = "INSERT INTO users (fname, email) VALUES(:fname, :email)";
    
    try{
        $db = new DbConnect();
        $conn = $db->connect();
        $stmt = $conn->prepare($sql);
        $stmt->bindParam("fname", $user->fname);
        $stmt->bindParam("email", $user->email);
        $stmt->execute();
        $user->id = $conn->lastInsertId();
        $db = null;
    
       echo json_encode($user);
    
    } catch(PDOException $e){
        echo '{"error":{"text":'. $e->getMessage().'}}';
    }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试