To make a DELETE request to remove a student using your API, follow the steps below. The DELETE endpoint in your controller is designed to delete a student with the specified id.
- Endpoint:
DELETE /api/students/{id} - Purpose: Delete a student with the given
id. - Headers:
Accept:application/jsonorapplication/xml(for the response format).
- Response:
200 OK: Success + message:"Student deleted successfully".404 Not Found: If the student with the giveniddoes not exist.
JSON Example:
curl -X DELETE "https://restapiapp.onrender.com/api/students/1" \
-H "Accept: application/json"XML Example:
curl -X DELETE "https://restapiapp.onrender.com/api/students/1" \
-H "Accept: application/xml"- Set the HTTP method to DELETE.
- Enter the URL:
https://restapiapp.onrender.com/api/students/1(replace1with the student’s ID). - Add headers:
Accept:application/jsonorapplication/xml.
- Send the request.
fetch('https://restapiapp.onrender.com/api/students/1', {
method: 'DELETE',
headers: {
'Accept': 'application/json'
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));- ID Matching: The
idin the URL path (/students/{id}) must correspond to an existing student. - Response:
200 OK: Success + message:"Student deleted successfully".404 Not Found: If the student with the giveniddoes not exist.
- Data Types: The
idis an integer (int), not a string.
- Invalid ID: If the
idis not found, the API will return a404 Not Foundresponse. - Non-Numeric ID: If the
idis not a valid integer (e.g.,abc), the API will return a400 Bad Requesterror.
- Request:
curl -X DELETE "https://restapiapp.onrender.com/api/students/1" \ -H "Accept: application/json"
- Response:
{ "message": "Student deleted successfully" }
- Request:
curl -X DELETE "https://restapiapp.onrender.com/api/students/999" \ -H "Accept: application/json"
- Response:
{ "error": "Student not found" }
- DELETE is a straightforward operation to remove a resource.
- Ensure the
idin the URL matches an existing student. - Use the
Acceptheader to specify the desired response format (JSON or XML).