Request():setParameter()
Set a parameter for the request body (used with POST/PUT/PATCH requests). This method allows you to add data that will be sent in the request body.
Signature
setParameter(key: string, value: string): Request
Parameters
Parameters | Type | Description |
---|---|---|
key | string | The parameter name |
value | string | The parameter value |
Returns
Request
- Returns the Request object for method chaining
Description
The setParameter()
method adds a key-value pair to the request body. This method is primarily used with POST, PUT, and PATCH requests where you need to send data to the server. You can call this method multiple times to add multiple parameters to the request body.
Examples
Basic POST with Parameters
-- Send form data with POST request
local response = Request("https://api.example.com/users")
:setParameter("name", "John Doe")
:setParameter("email", "john@example.com")
:setParameter("age", "30")
:post()
POST Request with JSON Content Type
-- Send JSON data with POST request
local response = Request("https://api.example.com/posts")
:setHeader("Content-Type", "application/json")
:setParameter("title", "My New Post")
:setParameter("body", "This is the content of my post")
:setParameter("userId", "1")
:post()
PUT Request with Parameters
-- Update a resource with PUT request
local response = Request("https://api.example.com/users/123")
:setHeader("Content-Type", "application/json")
:setParameter("name", "Jane Smith")
:setParameter("email", "jane@example.com")
:setParameter("status", "active")
:put()
PATCH Request with Partial Updates
-- Partially update a resource with PATCH
local response = Request("https://api.example.com/users/456")
:setHeader("Content-Type", "application/json")
:setParameter("email", "newemail@example.com")
:setParameter("lastUpdated", "2023-12-01")
:patch()
Form Submission
-- Submit a login form
local response = Request("https://example.com/login")
:setHeader("Content-Type", "application/x-www-form-urlencoded")
:setParameter("username", "myusername")
:setParameter("password", "mypassword")
:setParameter("remember", "true")
:post()
Search Query with POST
-- Send search parameters via POST
local response = Request("https://api.example.com/search")
:setParameter("query", "lua programming")
:setParameter("limit", "10")
:setParameter("sort", "relevance")
:setParameter("filters", "books,tutorials")
:post()
local results = response:getData()
local status = response:getResponseCode()
if status == 200 then
print("Search results: " .. results)
else
print("Search failed with status: " .. status)
end
Complex Data Structure
-- Create a complex object with multiple nested parameters
local response = Request("https://api.example.com/orders")
:setHeader("Content-Type", "application/json")
:setParameter("customerId", "12345")
:setParameter("productId", "67890")
:setParameter("quantity", "2")
:setParameter("shippingAddress", "123 Main St, City, State")
:setParameter("paymentMethod", "credit_card")
:post()
Parameter Types
When using setParameter()
, all values are treated as strings. Here are common use cases:
Parameter Type | Example | Notes |
---|---|---|
Text | setParameter("name", "John Doe") | Regular text strings |
Numbers | setParameter("age", "30") | Numbers should be passed as strings |
Dates | setParameter("date", "2023-12-01") | Use ISO format for dates |
Booleans | setParameter("active", "true") | Pass "true" or "false" as strings |
JSON | setParameter("data", '{"key":"value"}') | JSON strings can be passed as parameters |
Notes
- This method is primarily used with POST, PUT, and PATCH requests
- All parameter values are treated as strings
- The method returns the Request object, allowing for method chaining
- Parameters are typically formatted according to the Content-Type header
- For GET requests, parameters should be included in the URL query string instead
- The actual format of the request body depends on the Content-Type header set with
setHeader()
- Multiple calls to
setParameter()
with the same key will override previous values