Skip to content

Instantly share code, notes, and snippets.

@Creativty
Created December 14, 2024 17:43
Show Gist options
  • Save Creativty/fae2ba37b78f207d6e0fa064d8eb4895 to your computer and use it in GitHub Desktop.
Save Creativty/fae2ba37b78f207d6e0fa064d8eb4895 to your computer and use it in GitHub Desktop.
A very basic http client in Odin using only the core library
package main
import "core:fmt"
import "core:log"
import "core:mem"
import "core:net"
import "core:os"
main :: proc() {
args := os.args
if len(args) != 2 {
fmt.println("Usage: ", args[0], "<address>")
os.exit(1)
}
address := args[1]
socket, err_dial := net.dial_tcp_from_hostname_and_port_string(address)
if err_dial != nil {
fmt.eprintln("error: could not dial", address, "with error", err_dial)
os.exit(1)
}
defer net.close(socket)
bytes_request := "GET / HTTP/1.1\r\n\r\n"
bytes_sent, err_write := net.send_tcp(socket, transmute([]u8)bytes_request)
if err_write != nil {
fmt.eprintln("error: failed during send to", address, "with error", err_write)
os.exit(1)
}
log.debugf("sent %d bytes\n", bytes_sent)
bytes_buff: [32 * mem.Kilobyte]byte
bytes_recv, err_recv := net.recv_tcp(socket, bytes_buff[:])
if err_recv != nil {
fmt.eprintln("error: failed during recv from", address, "with error", err_recv)
os.exit(1)
}
msg := bytes_buff[:bytes_recv]
fmt.println(cast(string)msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment