RESTp - POST request example Rest API to perform POST operation

RESTp insert function provides a simplest way to insert the data into database by passing the data in form of associative array. You can easily add a record using the POST method and by passing POST data. Talking strictly in terms of REST, POST methods are used to create a new resource into the collection of resources. If you perform a `POST` request, the server creates a new entry in the database and tells you whether the creation is successful. In other words, a `POST` request performs an `CREATE` operation. To create a new record in a particular table, you need to call that end point and pass POST data. On success, it returns the primary key value of record inserted.

Please note that if you are sending data as json format using json_encode function then you must specify the content type header ('Content-Type:application/json') as default content type is post array.

  
                            //Dummy data to insert.
                            $data = array("data" => array("order_date" => date("Y-m-d"), "order_amount" => 800, "order_status" => "Completed"));
                            //Option 1: Convert data array to json if you want to send data as json
                            $data = json_encode($data);
                            //Option 2: else send data as post array.
                            //$data = urldecode(http_build_query($data));
                            /****** curl code ****/
                            //init curl
                            $ch = curl_init();
                            // URL to be called
                            curl_setopt($ch, CURLOPT_URL, "http://pdocrud.com/RESTP/api/orders/");
                            //set post TRUE to do a regular HTTP POST
                            curl_setopt($ch, CURLOPT_POST, 1);
                            //set http headers - if you are sending as json data (i.e. option 1) else comment this 
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
                            //send post data
                            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                            //return as output instead of printing it
                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                            //execute curl request
                            $result = curl_exec($ch);
                            //close curl connection
                            curl_close($ch);
                            //print result
                            print_r($result);
                        
  
                                jQuery.ajax({
                                type: "post",
                                url: "http://pdocrud.com/RESTP/api/orders/",
                                data : {data:{order_date: "2018-09-09",
                                        order_amount:800,
                                        order_status:"Completed"}},
                                success: function (response) {
                                    console.log(response);
                                    jQuery("#js_output").text(response);
                                }
                            });