Context
Sending a DELETE Request in HarmonyOS Using HTTP and Remote Communication APIs
Description
Developers often need to interact with remote services to manage data. One common operation is sending a DELETE request to remove a resource from a server. While HarmonyOS provides two different communication approaches—HTTP-based communication using Network Kit and Remote Communication using RCP (Remote Communication Protocol)—sending a DELETE request is not as straightforward as it may seem. Each approach has its own method signatures, lifecycle management, and error-handling considerations. This creates a challenge for developers who need a consistent, efficient, and secure way to implement DELETE operations in HarmonyOS applications. Understanding how to correctly configure and execute a DELETE request using both APIs is critical for building responsive and robust distributed apps.
Approach
Remote Communication Kit
const deleteURL = `https://jsonplaceholder.typicode.com/todos/${this.ids}`;
const sessions = rcp.createSession();
sessions.delete(deleteURL)
.then((response) => {
this.postStatus = 'Success!';
this.deletedDataUrl = this.ids;
console.error(`DELETE success: ${JSON.stringify(response)}`);
})
.catch((err: BusinessError) => {
console.error('DELETE error:', err.message);
});
Network Kit
const deleteURL = `https://jsonplaceholder.typicode.com/todos/${this.ids}`;
let httpRequest = http.createHttp();
httpRequest.request(
deleteURL,
{
method: http.RequestMethod.DELETE,
header:{
'Content-Type': 'application/json'
}
},
(err: Error | undefined, res: http.HttpResponse) => {
if(!err) {
this.deletedDataUrl = deleteURL;
console.error(`DELETE success: ${JSON.stringify(res.result)}`);
} else {
console.error('DELETE error:', err.message);
}
})
The two functions, deleteTransferSyn and delete, demonstrate two different approaches to sending a DELETE HTTP request in HarmonyOS. The deleteTransferSyn function uses RCP (Remote Communication Protocol) via rcp.createSession(), which is suitable for communication between distributed devices or when leveraging HarmonyOS's remote communication capabilities. It handles the request using modern Promise-based syntax (then/catch) and integrates smoothly with asynchronous logic. On the other hand, the delete function uses standard HTTP communication via http.createHttp(), which is more appropriate for traditional web-based RESTful APIs. This method relies on a callback-style pattern rather than Promises, which can be less intuitive in complex flows. While both approaches achieve the same goal—deleting a resource from a server—they differ in architecture, use cases, and syntax style. RCP is ideal for inter-device operations within the HarmonyOS ecosystem, whereas http.createHttp() is more generic and useful for interacting with typical HTTP services.
Key Takeaways
This code compares two methods for sending a DELETE request in HarmonyOS: one using RCP (Remote Communication Protocol) and the other using the HTTP module.
- RCP, built on top of Network Kit, enables efficient communication between distributed devices within the HarmonyOS ecosystem and supports a Promise-based syntax for cleaner asynchronous handling.
- the HTTP-based method is designed for standard web APIs and uses a callback-style approach to process responses. While both achieve the same functional result—removing a remote resource—they are suited to different use cases: RCP for inter-device communication and http.createHttp() for conventional RESTful web services.
Top comments (0)