【发布时间】:2019-02-13 11:16:21
【问题描述】:
我在 postman 中测试了许多 REST API,但从未遇到过包含 $_REQUEST['method'] 的 API,它决定调用什么方法。现在我的问题是如何在邮递员上测试这个 API。如何在邮递员中传递$_REQUEST['method'] 姓名。
这是我的 PHP 代码
<?php
include_once('config.php');
if(isset($_REQUEST['method'])){
// echo '<pre>';
if($_POST['method']=='create'){
$name= $_POST['name'];
$location = $_POST['location'];
// $images = null;
$rating = $_POST['rating'];
$specility = $_POST['specility'];
$file = rand(1000,100000)."-".time().'-'.$_FILES['image']['name'];
$file_loc = $_FILES['image']['tmp_name'];
$file_size = $_FILES['image']['size'];
$file_type = $_FILES['image']['type'];
$path_name="images/".$file;
move_uploaded_file($file_loc,$path_name);
$query = "INSERT into `restaurant` (name, location, image, rating,specility) VALUES ('$name', '$location', '$path_name', '$rating','$specility')";
$result = mysqli_query($con,$query);
if($result){
echo json_encode(['status'=>'success','response'=>'Restaurant created successfuly']);
}else{
echo json_encode(['status'=>'failed','response'=>'Restaurant details are not proper']);
}
}
if($_POST['method']=='list'){
$query = "SELECT * FROM `restaurant`";
$result = mysqli_query($con,$query);
if(mysqli_num_rows($result)>0){
$data=mysqli_fetch_assoc($result);
echo json_encode(['status'=>'success','response'=>$data]);
}else{
echo json_encode(['status'=>'failed','response'=>'No data found']);
}
}
}else{
echo json_encode(['status'=>'failed','response'=>'Something went wrong']);
}
我还在服务器上托管了这个 API。我不知道我在互联网上搜索什么来解决这个问题。请参阅下面的图片,出于安全原因,我更改了网址。
请告诉我该怎么做。
【问题讨论】:
-
你能显示你的请求参数吗?
-
哪些参数?
-
请求参数
-
在脚本中根据需要添加值为“create”或“list”的键“method”。
-
我应该在正文部分下的
form-data中添加它