Skip to main content

Request():delete()

Execute a DELETE request and return a Response object. This method is used to delete a resource from a server.

Signature

delete(): Response

Returns

Response - The response object containing the server response

Description

The delete() method sends an HTTP DELETE request to the URL specified when creating the Request object. DELETE requests are typically used to remove existing resources from a server, such as deleting a user, post, or any other data entity.

Examples

Basic DELETE Request

-- Delete a user with ID 123
local response = Request("https://api.example.com/users/123"):delete()

DELETE Request with Authentication

-- Delete a post with authorization
local response = Request("https://api.example.com/posts/456")
:setHeader("Authorization", "Bearer your-token-here")
:setHeader("Accept", "application/json")
:delete()

Processing DELETE Response

-- Delete a comment and handle the response
local response = Request("https://api.example.com/comments/789")
:setHeader("Authorization", "Bearer your-token-here")
:delete()

local data = response:getData()
local statusCode = response:getResponseCode()

if statusCode == 204 then
print("Comment deleted successfully! (No content)")
elseif statusCode == 200 then
print("Comment deleted successfully!")
print("Response: " .. data)
else
print("Failed to delete comment. Status: " .. statusCode)
end

DELETE with Additional Headers

-- Delete a resource with custom headers
local response = Request("https://api.example.com/files/document.pdf")
:setHeader("Authorization", "Bearer token123")
:setHeader("X-Delete-Reason", "User requested deletion")
:setHeader("Accept", "application/json")
:delete()

local status = response:getResponseCode()
if status == 200 or status == 204 then
print("File deleted successfully")
else
print("Delete failed with status: " .. status)
end

Conditional Delete

-- Delete only if resource hasn't been modified
local response = Request("https://api.example.com/data/123")
:setHeader("If-Match", "etag-value-here")
:setHeader("Authorization", "Bearer token")
:delete()

local status = response:getResponseCode()
if status == 412 then
print("Delete failed: Resource has been modified")
elseif status == 200 or status == 204 then
print("Resource deleted successfully")
end

Notes

  • DELETE requests typically don't have a request body
  • Successful DELETE operations usually return:
    • 200 OK with a response body describing the deletion
    • 202 Accepted if the deletion is processed asynchronously
    • 204 No Content if the deletion was successful with no response body
  • The method returns a Response object which you can use to access the response data and status code
  • Always check the response status code to ensure the request was successful
  • DELETE requests are idempotent - making the same request multiple times should have the same effect (the resource is deleted)
  • Be careful when using DELETE requests as they permanently remove data