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

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化