Function url::post()

← Back to Class url



Module urlClass url → post()

Description

Perform HTTP POST request

url::post(url, request_params = "", multipart = false, headers = nothing, downloaded_to = "", progress_callback = nothing)


Parameters


Return Value


Usage Example

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