Skip to main content

Response

The Response object contains the result of an HTTP request made with the Request module. It provides access to the response data, status code, and other metadata returned by the server.

Overview

Response objects are returned by HTTP request methods such as get(), post(), put(), patch(), and delete(). They encapsulate the server's response and provide methods to access the response content and metadata.

Methods

getData(): string

Get the response body content as a string.

Returns

string - The response body content

getResponseCode(): number

Get the HTTP status code from the response.

Returns

number - The HTTP status code (e.g., 200, 404, 500)

Common Usage Pattern

-- Make a request and handle the response
local response = Request("https://api.example.com/data"):get()

-- Get the status code to check if the request was successful
local statusCode = response:getResponseCode()

if statusCode == 200 then
-- Request was successful, get the data
local data = response:getData()
print("Response data: " .. data)
else
-- Request failed, handle the error
print("Request failed with status: " .. statusCode)
local errorData = response:getData()
if errorData and errorData ~= "" then
print("Error details: " .. errorData)
end
end

HTTP Status Code Categories

Status RangeMeaningTypical Response
200-299SuccessContains requested data or confirmation
300-399RedirectionUsually empty or contains redirect info
400-499Client ErrorContains error description
500-599Server ErrorContains error description

Response Data Formats

Response data is always returned as a string, regardless of the original format:

  • JSON: "{\"key\":\"value\"}" - May need parsing if you have a JSON parser
  • XML: "<?xml version=\"1.0\"?><root>...</root>" - XML content as string
  • Plain Text: "Simple text response" - Direct text content
  • HTML: "<html><body>...</body></html>" - HTML content as string
  • Empty: "" - No content (common with 204 responses)

Best Practices

  1. Always check status code first before processing response data
  2. Handle empty responses gracefully, especially with 204 status codes
  3. Check response content even for error responses as they may contain useful information
  4. Be prepared for network errors that may prevent a response object from being created

Examples

Basic Response Handling

local response = Request("https://api.example.com/users/123"):get()
local status = response:getResponseCode()

if status == 200 then
local userData = response:getData()
print("User data: " .. userData)
else
print("Failed to get user data. Status: " .. status)
end

Error Handling

local response = Request("https://api.example.com/invalid"):get()
local status = response:getResponseCode()

if status == 404 then
print("Resource not found")
elseif status == 401 then
print("Authentication required")
elseif status >= 500 then
print("Server error: " .. status)
local errorDetails = response:getData()
print("Error details: " .. errorDetails)
else
print("Unexpected status code: " .. status)
end

Working with Different HTTP Methods

-- GET request
local getResponse = Request("https://api.example.com/data"):get()
print("GET status: " .. getResponse:getResponseCode())

-- POST request
local postResponse = Request("https://api.example.com/data")
:setParameter("key", "value")
:post()
print("POST status: " .. postResponse:getResponseCode())

-- DELETE request
local deleteResponse = Request("https://api.example.com/data/123"):delete()
print("DELETE status: " .. deleteResponse:getResponseCode())