url::post()
Perform HTTP POST request
url::post(url, request_params = "", multipart = false, headers = nothing, downloaded_to = "", progress_callback = nothing)
url
(string) — A URL stringrequest_params
(string|object, optional) — Params to be requested. The default value is ""multipart
(boolean, optional) — Use multipart/form-data rather than application/x-www-form-urlencoded as the POST data request. When multipart
is set to true
, then the request_params
must be an object. The default value is false
headers
(object, optional) — HTTP headers. The default value is nothing
downloaded_to
(string, optional) — If downloaded_to
is set, then the response body will be saved to a file. The default value is ""progress_callback
(function_call, optional) — A function call to catch current download and upload progress. The default value is nothing
response
— Returns response
object
import url
' Simple HTTP POST example
response = url.post("https://faruq.id/misc/post.php", {
"id": "A0110",
"name": "Sarah Zaira",
"age": 23
})
if response.code == 200
writeln(response.body) ' Success
elseif response.code != 0
writeln("Error: HTTP " & response.code) ' HTTP error
else
writeln(response.error_string) ' Connection or other error
endif
' HTTP POST with multipart form data
params = {
"id": "A23",
"name": "Clara Sarah",
"age": 23
}
response = url.post("https://faruq.id/misc/post.php", params, true)
if response.code == 200
writeln(response.body)
elseif response.code != 0
writeln("Error: HTTP " & response.code)
else
writeln(response.error_string)
endif
' Upload files using multipart form data (we will use function file_data())
params = {
"id": "U1234",
"file1": file_data("data.txt"), ' Upload a file
"file2": file_data("some.zip") ' Upload another file
}
response = url.post("https://faruq.id/misc/post.php", params, true)
if response.code == 200
writeln("Upload complete!")
else
writeln(response.error_string)
endif
' JSON request & response example
import json
request = json.encode({
"id": 23,
"name": "Clara Zaira",
"age": 20,
"hobbies": [
"Reading",
"Travelling",
"Music"
]
})
response = url.post("https://faruq.id/misc/json.php", request, false, {"Content-Type": "application/json"})
if response.code == 200
writer(json.decode(response.body))
elseif response.code != 0
writeln("Error: HTTP " & response.code)
else
writeln(response.error_string)
endif