FreeBSD CURL troubleshooting with POST/GET/PUT/DELETE methods and json files

Our goals is troubleshooting of GET and POST methods with CURL. We already configured two servers:

  1. frfs.unixmen.com (We are sending CURL requests from this machine)
  2. asterisk.unixmen.com (This WEB server takes POST and GET requests)

cURL

It is assumed that already downloaded and installed FAMP to asterisk.unixmen.com server. Our web server is working with SSL and for tests we will copy already generated asterisk.pem key to our frfs.unixmen.com server /root folder. In the same WEB server already MySQL installed and configured.

For MySQL database our scheme will be as follows:
mysql> CREATE DATABASE websayt;
mysql> use websayt
mysql> CREATE TABLE Persons (ID int, FirstName varchar(45), LastName varchar(45), eMail varchar(45), mPhone varchar(15), PRIMARY KEY (ID) );
mysql> GRANT ALL PRIVILEGES ON websayt.* TO ‘websayt‘@’localhost’ IDENTIFIED BY ‘freebsd‘;
mysql> FLUSH PRIVILEGES;

Imagine that, our PUBLIC_HTML folder path is /usr/local/www/asterisk. In this folder we will create two files. postapi.php and getapi.php. We will use the getapi.php file with curl for select information from database. With postapi.php file we will insert needed information to our database.

The content of postapi.php file will be as follows:
<?php
 
$data_back = json_decode(file_get_contents(‘php://input’));
 
// set json string to php variables
$id = $data_back->{“id”};
$firstName = $data_back->{“firstName”};
$lastName = $data_back->{“lastName”};
$email = $data_back->{“email”};
$mPhone = $data_back->{“mPhone”};
 
//Database connection
$servername = “localhost“;
$username = “websayt“;
$password = “freebsd“;
$dbname = “websayt“;
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die(“Connection failed: ” . $conn->connect_error);
}
 
$sql = “INSERT INTO Persons (id,firstname,lastname,email,mphone) VALUES (‘$id’,’$firstName’,’$lastName’,’$email’,’$mPhone’)”;
// set header as json
//header(“Content-type: application/json”);
 
 
if ($conn->query($sql) === TRUE) {
    echo “New record created successfully”;
} else {
    echo “Error: ” . $sql . “<br>” . $conn->error;
}
 
$conn->close();
?>

The content of getapi.php file will be as follows:
<?php
 
$mPhone = $data_back->{“mPhone”};
 
$servername = “localhost“;
$username = “websayt“;
$password = “freebsd“;
$dbname = “websayt“;
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die(“Connection failed: ” . $conn->connect_error);
}
 
$sql = “select $_GET[param] from Persons where id = $_GET[id]”;
 
//echo $sql;
// set header as json
//header(“Content-type: application/json”);
 
$result = $conn->query($sql);
if(!$result)
{
        die(‘Connection error’);
}
if($result->num_rows>0)
{
while($row = $result->fetch_assoc())
{
        echo $row[$_GET[param]].PHP_EOL;
        break;
}
}else{
echo “0 results”;
}
$conn->close();
?>

And now we will use frfs.unixmen.com server for test with CURL. We will do GET and POST requests.

Add the following lines to the /root/body.json file. In this lines we telling insert all this information’s to this columns with id 1:
{
        “id“: 1,
        “firstName“: “Eziyyet”,
        “lastName“: “Zulumov”,
        “email“: “ezum@email.com”,
        “mPhone“: “0552592433”
}

Use the POST method:
curl -i -v3 –cacert /root/asterisk.pem -X POST -d @/root/body.json -H “Content-Type: application/json” https://asterisk.unixmen.com/postapi.php
-i – Add HTTP-Header in output
-v3 – More detail verbose level (3th level)
–cacert – The certificate which have got from asterisk.unixmen.com we will use here as argument.
-X – Set used HTTP method. In previous command we used POST.
-d – Send the json codes in the /root/body.json file with POST request to the HTTP server. Be careful, before file path must be ‘@’ symbol (otherwise will not work).
-H – When we send request to the HTTP server, with this option we define attach header. In our case as we used json we will use “Content-Type: application/json”

Use the GET method:
curl -i -v3 –cacert /root/asterisk.pem -X GET “https://asterisk.unixmen.com/getapi.php?id=1&param=mphone”
-X – And here we telling use the https://asterisk.unixmen.com/getapi.php URL with GET method and select information in column id with number 1 from mphone column.

If we will turn off out logging and HTTP header the result of command will be as follows:
curl –cacert /root/asterisk.pem -X GET “https://asterisk.unixmen.com/getapi.php?id=1&param=firstname”
Eziyyet

With the same way using PUT and DELETE methods we can update and delete any column of table from our MySQL database. For that we will create two files in our PUBLIC_HTML folder /usr/local/www/asterisk with names putapi.php and deleteapi.php. The content of /usr/local/www/asterisk/putapi.php file will be as follows:
<?php
 
$json = file_get_contents(‘php://input’);
$data_back = json_decode($json);
$id = $data_back->{“id”};
$paramName = $data_back->{“paramName”};
$paramValue = $data_back->{“paramValue”};
 
$servername = “127.0.0.1“;
$username = “websayt“;
$password = “freebsd“;
$dbname = “websayt“;
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die(“Connection failed: ” . $conn->connect_error);
}
 
$sql = “UPDATE Persons SET $paramName = ‘$paramValue’ WHERE id = $id”;
 
// set header as json
//header(“Content-type: application/json”);
 
if ($conn->query($sql) === TRUE) {
    echo “Update was successfull.”;
} else {
    echo “Error: ” . $sql . “<br>” . $conn->error;
}
 
$conn->close();
 
//echo $id.” “.$paramName.” “.$paramValue.PHP_EOL;
 
?>

The content of /usr/local/www/asterisk/deleteapi.php file will be as follows:
<?php
 
$json = file_get_contents(‘php://input’);
$data_back = json_decode($json);
$id = $data_back->{“id”};
 
$servername = “127.0.0.1“;
$username = “websayt“;
$password = “freebsd“;
$dbname = “websayt“;
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die(“Connection failed: ” . $conn->connect_error);
}
 
$sql = “delete from Persons where id = $id”;
 
if ($conn->query($sql) === TRUE) {
    echo “Delete was successfull.”;
} else {
    echo “Error: ” . $sql . “<br>” . $conn->error;
}
 
$conn->close();
 
?>

For test of this two methods in our frfs.unixmen.com server we will create put.json and delete.json files and will add contents to this files which we need. Create /root/put.json file and add content as below (Change the value of paramValue column where ID is 10 to zulum@gmail.com):
{
    “id“: 10,
    “paramName“: “email”,
    “paramValue“: “zulum@gmail.com”
}

Create /root/delete.json file and add content as below (Send the line with ID 3 to the DELETE method which, will delete this line):
{
    “id“: 3
}

Testing:
curl –cacert /root/asterisk.pem -X GET -H “Accept: application/json” -d @/root/put.json https://asterisk.unixmen.com/putapi.php
-X – Using GET method

curl –cacert /root/asterisk.pem -X DELETE -H “Accept: application/json” -d @/root/delete.json https://asterisk.unixmen.com/deleteapi.php
-X –Using DELETE method