Skip to main content

Request():patch()

Execute a PATCH request and return a Response object. This method is used to partially update a resource on a server.

Signature

patch(): Response

Returns

Response - The response object containing the server response

Description

The patch() method sends an HTTP PATCH request to the URL specified when creating the Request object. PATCH requests are typically used to make partial updates to existing resources, modifying only the fields that are provided in the request body.

Use setParameter() to add data to the request body before calling patch().

Examples

Basic PATCH Request

-- Update only the email field of a user
local response = Request("https://api.example.com/users/123")
:setParameter("email", "newemail@example.com")
:patch()

PATCH Request with JSON Headers

-- Partially update a post with multiple fields
local response = Request("https://api.example.com/posts/456")
:setHeader("Content-Type", "application/json")
:setHeader("Authorization", "Bearer your-token-here")
:setParameter("title", "Updated Post Title")
:setParameter("status", "published")
:patch()

Processing PATCH Response

-- Update user profile and handle response
local response = Request("https://api.example.com/users/789")
:setHeader("Content-Type", "application/json")
:setHeader("Authorization", "Bearer token123")
:setParameter("bio", "Updated bio information")
:setParameter("website", "https://mywebsite.com")
:patch()

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

if statusCode == 200 then
print("User updated successfully!")
print("Updated data: " .. data)
elseif statusCode == 204 then
print("User updated successfully! (No content returned)")
else
print("Failed to update user. Status: " .. statusCode)
end

Conditional PATCH Update

-- Update only if resource hasn't been modified
local response = Request("https://api.example.com/articles/123")
:setHeader("Content-Type", "application/json")
:setHeader("If-Match", "etag-value-here")
:setHeader("Authorization", "Bearer token")
:setParameter("content", "Updated article content")
:setParameter("lastModified", "2023-12-01")
:patch()

local status = response:getResponseCode()
if status == 412 then
print("Update failed: Resource has been modified by another user")
elseif status == 200 or status == 204 then
print("Article updated successfully")
end

PATCH with Field Selection

-- Update specific fields while leaving others unchanged
local response = Request("https://api.example.com/products/456")
:setHeader("Content-Type", "application/json")
:setParameter("price", "29.99")
:setParameter("stock", "150")
:setParameter("discount", "10")
:patch()

local updatedProduct = response:getData()
print("Product updated: " .. updatedProduct)

Notes

  • Use setParameter() to add data to the request body before calling patch()
  • PATCH requests should only contain the fields you want to update, not the entire resource
  • PATCH is different from PUT in that PATCH performs partial updates, while PUT replaces the entire resource
  • Successful PATCH operations typically return:
    • 200 OK with the updated resource in the response body
    • 204 No Content if the update was successful but no response body is needed
  • 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
  • PATCH requests are not necessarily idempotent - making the same request multiple times may have different effects depending on the implementation