Request():get()
Execute a GET request and return a Response object. This method is used to retrieve data from a specified URL.
Signature
get(): Response
Returns
Response
- The response object containing the server response
Description
The get()
method sends an HTTP GET request to the URL specified when creating the Request object. GET requests are typically used to retrieve data from a server without modifying any existing data.
Examples
Basic GET Request
-- Create a GET request to an API endpoint
local response = Request("https://api.example.com/users"):get()
GET Request with Custom Headers
-- GET request with authentication header
local response = Request("https://api.example.com/protected-data")
:setHeader("Authorization", "Bearer your-token-here")
:setHeader("Accept", "application/json")
:get()
Processing GET Response
-- Make the request and handle the response
local response = Request("https://jsonplaceholder.typicode.com/posts/1"):get()
-- Get the response data as string
local data = response:getData()
-- Get the HTTP status code
local statusCode = response:getResponseCode()
if statusCode == 200 then
print("Request successful!")
print("Response data: " .. data)
else
print("Request failed with status: " .. statusCode)
end
GET Request with Query Parameters
-- For GET requests with query parameters, include them in the URL
local response = Request("https://api.example.com/search?q=lua&limit=10"):get()
local results = response:getData()
Notes
- GET requests should not have a request body. If you need to send data, use POST() instead
- 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
- GET requests are idempotent, meaning making the same request multiple times should have the same effect