A very basic wrapper around libcurl that works on Linux and Windows (and maybe macos).
- For Linux users ensure a dev version of
libcurlis installed on your system.
import "core:fmt"
import http "http-client"
main :: proc() {
url := "https://odin-lang.org/"
response, code := http.http_get(url)
defer http.response_free(response)
if code != .E_OK {
fmt.panicf("Curl error: %s", http.curl_strerror(code))
}
fmt.println(response)
}import "core:encoding/json"
import "core:fmt"
import http "http-client"
Payload :: struct {
value: int,
message: string,
}
main :: proc() {
url := "https://example.com"
data := Payload {
value = 1,
message = "Payload message",
}
msg, _ := json.marshal(data)
response, code := http.http_post_json(url, string(msg))
defer http.response_free(response)
if code != .E_OK {
fmt.panicf("Curl error: %s", http.curl_strerror(code))
}
fmt.println(response)
}import "core:encoding/json"
import "core:fmt"
import http "http-client"
Payload :: struct {
value: int,
message: string,
}
main :: proc() {
url := "https://example.com"
data := Payload {
value = 1,
message = "Payload message",
}
msg, _ := json.marshal(data)
client: http.Http_Client
defer http.client_free(&client)
err := http.client_init(&client, url)
if err != nil {
fmt.panicf("Error initialising Http_Client: %v", err)
}
client.method = .POST
client.body = string(msg)
client.headers["Content-Type"] = {"application/json"}
code := http.client_run(&client)
if code != .E_OK {
fmt.panicf("Curl error: %s", http.curl_strerror(code))
}
fmt.println(client.response)
}