|
// retoor <retoor@molodetz.nl>
|
|
|
|
import "http" for Http, HttpResponse, Url
|
|
|
|
System.print("=== HTTP Module Demo ===\n")
|
|
|
|
System.print("--- URL Parsing ---")
|
|
var urls = [
|
|
"http://example.com",
|
|
"http://example.com:8080",
|
|
"http://example.com/path/to/resource",
|
|
"http://example.com/search?q=hello&limit=10"
|
|
]
|
|
|
|
for (urlStr in urls) {
|
|
var url = Url.parse(urlStr)
|
|
System.print("URL: %(urlStr)")
|
|
System.print(" scheme: %(url.scheme), host: %(url.host), port: %(url.port)")
|
|
System.print(" path: %(url.path), query: %(url.query)")
|
|
System.print("")
|
|
}
|
|
|
|
System.print("--- Simple GET Request ---")
|
|
System.print("Fetching http://httpbin.org/get...")
|
|
var response = Http.get("http://httpbin.org/get")
|
|
System.print("Status: %(response.statusCode) %(response.statusText)")
|
|
System.print("OK: %(response.ok)")
|
|
|
|
System.print("\n--- Response Headers ---")
|
|
var count = 0
|
|
for (header in response.headers) {
|
|
if (count < 5) System.print(" %(header.key): %(header.value)")
|
|
count = count + 1
|
|
}
|
|
|
|
System.print("\n--- Response Body (first 300 chars) ---")
|
|
var body = response.body
|
|
if (body.count > 300) body = body[0...300] + "..."
|
|
System.print(body)
|
|
|
|
System.print("\n--- Getting Specific Header ---")
|
|
var contentType = response.header("Content-Type")
|
|
System.print("Content-Type: %(contentType)")
|
|
|
|
System.print("\n--- GET with Query Parameters ---")
|
|
var searchResponse = Http.get("http://httpbin.org/get?name=wren&version=1.0")
|
|
System.print("Status: %(searchResponse.statusCode)")
|
|
System.print("Body contains 'wren': %(searchResponse.body.contains("wren"))")
|
|
|
|
System.print("\n--- POST Request ---")
|
|
System.print("Posting to httpbin.org...")
|
|
var postResponse = Http.post("http://httpbin.org/post", "Hello from Wren!")
|
|
System.print("Status: %(postResponse.statusCode)")
|
|
System.print("Body contains 'Hello': %(postResponse.body.contains("Hello"))")
|
|
|
|
System.print("\n--- Custom Headers ---")
|
|
var headersResponse = Http.get("http://httpbin.org/headers", {
|
|
"X-Custom-Header": "WrenTest",
|
|
"Accept": "application/json"
|
|
})
|
|
System.print("Status: %(headersResponse.statusCode)")
|
|
System.print("Body contains 'WrenTest': %(headersResponse.body.contains("WrenTest"))")
|
|
|
|
System.print("\n--- Checking Response Status ---")
|
|
var notFoundResponse = Http.get("http://httpbin.org/status/404")
|
|
System.print("404 response OK: %(notFoundResponse.ok)")
|
|
System.print("Status code: %(notFoundResponse.statusCode)")
|
|
|
|
var okResponse = Http.get("http://httpbin.org/status/200")
|
|
System.print("200 response OK: %(okResponse.ok)")
|
|
|
|
System.print("\n--- HTTP Response Object ---")
|
|
System.print(response.toString)
|
|
|
|
System.print("\n--- Practical Example: Get UUID ---")
|
|
var uuidResponse = Http.get("http://httpbin.org/uuid")
|
|
System.print("Status: %(uuidResponse.statusCode)")
|
|
var uuidBody = uuidResponse.body
|
|
if (uuidBody.contains("uuid")) {
|
|
System.print("UUID response received successfully")
|
|
System.print("Body: %(uuidBody)")
|
|
}
|
|
|
|
System.print("\n--- HTTPS Request ---")
|
|
System.print("Fetching https://httpbin.org/get...")
|
|
var httpsResponse = Http.get("https://httpbin.org/get")
|
|
System.print("Status: %(httpsResponse.statusCode) %(httpsResponse.statusText)")
|
|
System.print("OK: %(httpsResponse.ok)")
|
|
System.print("Body contains 'origin': %(httpsResponse.body.contains("origin"))")
|
|
|
|
System.print("\n--- HTTPS POST Request ---")
|
|
var httpsPost = Http.post("https://httpbin.org/post", {"message": "Hello from Wren over HTTPS!"})
|
|
System.print("Status: %(httpsPost.statusCode)")
|
|
System.print("Body contains 'Hello': %(httpsPost.body.contains("Hello"))")
|