fetching http from url
Published .. 28-06-2024
Type ....... article
Tags ....... v, http
Type ....... article
Tags ....... v, http
Somehow it feels like V's standard library are too easy to use:
module main
import net.http
import time
fn main() {
url := 'https://infoserv-hosting.dk/'
f := http.FetchConfig{
url: url
allow_redirect: false
user_agent: 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0'
}
println('Fetching ${url}')
timer := time.new_stopwatch()
res := http.fetch(f) or {
println('Failed in ${timer.elapsed().milliseconds()} ms, error was : ${err}')
return
}
if res.status_code == 200 {
println('Good response ${res.status_code}, received ${res.body.len} bytes in ${timer.elapsed().milliseconds()} ms')
} else {
loc := res.header.get(.location) or { '' }
println('No data, got response ${res.status_code} (location: ${loc}) in ${timer.elapsed()} ms')
}
}
In some situations the default settings, fx that the content of the body is read into memory in its interety, might not be appropriate. But for much of what I do, it's just dandy.