Part 3: Testing API
This application provides the following endpoints:
Method | URL | Action |
---|---|---|
POST | /api/publishers | Add a new publisher |
GET | /api/publishers | Retrieve all publishers |
DELETE | /api/publishers | Delete all publisher |
PUT | /api/publishers/{id} | Update a publisher by :id |
DELETE | /api/publishers/{id} | Delete a publisher by :id |
POST | /api/publishers/{publisherId}/advs | Create/Add an advertisement for a publisher |
GET | /api/publishers/{publisherId}/advs | Retrieve all advertisements of a publisher |
GET | /api/advs/{advsId}/publishers | Retrieve all publishers of an advertisement |
GET | /api/advs/{id} | Retrieve a specific advertisement by id |
PUT | /api/advs/{id} | Update a specific advertisement by id |
DELETE | /api/advs/{id} | Delete a specific advertisement by id |
DELETE | /api/publishers/{publisherId}/advs/{advsId} | Delete a specific advertisement by id from a specific publisher |
Examples
Add a publisher (Postman):
Add an advertisement to publisher with id=1
Add a publisher (PHP - cURL):
Note: You will need an HTTP sever to run these PHP files. Navigate to the folder where these files are located and start the server as follow and then in your browser go to http://127.0.0.1:9000
to view the results:
cd path/to/your/app or PHP files php -S 127.0.0.1:9000
<?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'localhost:8080/api/publishers', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>'{ "name":"Media.net", "publisher_address":"New York, USA", "publisher_contact":"info@media.net" }', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json' ), )); $response = curl_exec($curl); curl_close($curl); echo $response; ?>
Retrieve all publishers (PHP - cURL):
<?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'localhost:8080/api/publishers', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', )); $response = curl_exec($curl); curl_close($curl); echo $response; ?>