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().'}}';
    }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集