/api/contacts/{id}

DELETE

Removes a contact from your QMail address book.

http://localhost:8080/api/contacts/{id}

Description

The `DELETE /api/contacts/{id}` endpoint permanently removes a contact from your address book. The operation is identified by the contact's unique user ID. If successful, the endpoint returns a 204 No Content response. If the contact is not found, it returns a 404 error.

Permanent Deletion

This operation cannot be undone. Once a contact is deleted, you will need to add them again if you want to restore them to your address book.

Success Response

A successful deletion returns HTTP 204 No Content with no response body. This is the standard REST pattern for successful DELETE operations.

Path Parameters

Parameter Type Required Description
id integer Required The user_id of the contact to delete (must be a positive integer)

Response

The endpoint returns different status codes depending on the outcome:

Response Status Codes

204 No Content Success
Contact was successfully deleted. No response body is returned.
404 Not Found Error
Contact with the specified ID does not exist.
400 Bad Request Error
Invalid ID format (must be a positive integer).

Success Response (204 No Content)

HTTP/1.1 204 No Content

(no response body)

Error Response (404 Not Found)

{
  "error": "Contact not found",
  "message": "No contact exists with ID 999"
}

Error Response (400 Bad Request)

{
  "error": "Invalid parameter",
  "message": "Contact ID must be a positive integer"
}

Try it out

http://localhost:8080/api/contacts/{id}

Examples

JavaScript (fetch)

const API_HOST = 'http://localhost:8080';

async function deleteContact(contactId) {
    try {
        const response = await fetch(`${API_HOST}/api/contacts/${contactId}`, {
            method: 'DELETE'
        });

        if (response.status === 204) {
            console.log(`Contact ${contactId} deleted successfully`);
            return true;
        } else if (response.status === 404) {
            const result = await response.json();
            console.error('Contact not found:', result.message);
            return false;
        } else {
            const result = await response.json();
            console.error('Error:', result.message || result.error);
            return false;
        }
    } catch (error) {
        console.error('Request failed:', error);
        return false;
    }
}

// Example usage
deleteContact(123);

cURL

# Delete a contact by ID
curl -X DELETE "http://localhost:8080/api/contacts/123"

# With verbose output to see status code
curl -v -X DELETE "http://localhost:8080/api/contacts/123"

# Store status code in variable
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE "http://localhost:8080/api/contacts/123")
echo "Status: $HTTP_STATUS"

Go

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

const ApiHost = "http://localhost:8080"

type ErrorResponse struct {
    Error   string `json:"error"`
    Message string `json:"message"`
}

func deleteContact(contactID int) (bool, error) {
    url := fmt.Sprintf("%s/api/contacts/%d", ApiHost, contactID)

    req, err := http.NewRequest("DELETE", url, nil)
    if err != nil {
        return false, err
    }

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return false, err
    }
    defer resp.Body.Close()

    if resp.StatusCode == http.StatusNoContent {
        fmt.Printf("Contact %d deleted successfully\n", contactID)
        return true, nil
    } else if resp.StatusCode == http.StatusNotFound {
        body, _ := ioutil.ReadAll(resp.Body)
        var errResult ErrorResponse
        json.Unmarshal(body, &errResult)
        fmt.Printf("Contact not found: %s\n", errResult.Message)
        return false, nil
    } else {
        body, _ := ioutil.ReadAll(resp.Body)
        var errResult ErrorResponse
        json.Unmarshal(body, &errResult)
        fmt.Printf("Error: %s\n", errResult.Message)
        return false, nil
    }
}

func main() {
    success, err := deleteContact(123)
    if err != nil {
        panic(err)
    }

    if success {
        fmt.Println("Operation completed successfully")
    } else {
        fmt.Println("Operation failed")
    }
}

Related Endpoints

/api/contacts (GET)

List all contacts with pagination and search.

/api/contacts (POST)

Add a new contact to your address book.

/api/data/contacts/popular

Get your most frequently contacted addresses.