v/vlib/http/http_client.v

23 lines
602 B
Go
Raw Normal View History

2019-08-21 19:04:06 +02:00
module http
import net
import strings
fn http_do(port int, method, host_name, path string) ?Response {
bufsize := 512
rbuffer := [512]byte
mut sb := strings.new_builder(100)
s := build_request_headers('', method, host_name, path)
client := net.dial( host_name, port) or { return error(err) }
client.send( s.str, s.len )
for {
readbytes := client.crecv( rbuffer, bufsize )
if readbytes < 0 { return error('http_do error reading response. readbytes: $readbytes') }
if readbytes == 0 { break }
sb.write( tos(rbuffer, readbytes) )
}
client.close()
return parse_response(sb.str())
}