Http:post(uri, [parameters])method
Send an asynchronous POST request to the connected HTTP server.
If the parameters table contains a File object, the request is sent as multipart/form-data. Otherwise, it is sent as application/x-www-form-urlencoded.
Parameters
uri
A stringrepresenting the resource on the server to POST parameters to.
[parameters]
An optional table (for multipart/form-data content) or a string (for raw data content) that contains parameters to be sent with the request.Each key of the provided table must be a string (representing the parameter name) and each value represents the value for the corresponding parameter (a string or a File object).
Return value
This method return a Task instance. Once the Task has finished, it will return two values :
- The first value is the Http instance used for the POST request.
- The second value is
nilin case of error or atablethat contains the server response, with the following fields "content": astringcontaining the body part of the HTTP response"elapsed": anumberrepresenting the elapsed time to get the response"status": anumbercontaining the HTTP response status"reason": astringcontaining HTTP response status text"ok": abooleanvalue indicating if the POST request succeeded"headers": atablecontaining a pair ofstrings(header field names of the HTTP response as key and content as values)"cookies": atablecontaining a pair ofstrings(cookies names of the HTTP response as key and content as values)
Example
--! luart-extensions
import net
local client = net.Http("https://reqbin.com")
local data = [[{
"Id": 12345,
"Customer": "John Smith",
"Quantity": 1,
"Price": 10.00
}]]
-- make a POST request, and after its terminated, call the provided function, to process the response
client:post("/echo/post/json", data).after = function (self, response)
if not response then
error(net.error)
end
print(response.content)
end
waitall()