Skip to content

Instantly share code, notes, and snippets.

@musaprg
Last active March 11, 2023 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musaprg/b73aa918816989db7c15b7b75d214ca4 to your computer and use it in GitHub Desktop.
Save musaprg/b73aa918816989db7c15b7b75d214ca4 to your computer and use it in GitHub Desktop.
http client test with zig on master channel
const std = @import("std");
const heap = std.heap;
const http = std.http;
const Uri = std.Uri;
const debug = std.debug;
pub fn main() anyerror!void {
var arena = heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var client: http.Client = .{.allocator = allocator};
defer client.deinit();
const uri = try Uri.parse("http://example.com");
debug.print("parsed schema: {s}\n", .{uri.scheme});
debug.print("parsed path: {s}\n", .{uri.path});
debug.print("parsed host: {s}\n", .{uri.host orelse ""});
var req = try client.request(uri, .{}, .{});
var buf_reader = std.io.bufferedReader(req.reader());
var in_stream = buf_reader.reader();
var buf: [1024]u8 = undefined;
while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
debug.print("{s}\n", .{line});
}
debug.print("Method: {s}\n", .{@tagName(req.headers.method)});
debug.print("Status code: {s}\n", .{@tagName(req.response.headers.status)});
debug.print("Version: {s}\n", .{@tagName(req.response.headers.version)});
}
@musaprg
Copy link
Author

musaprg commented Mar 5, 2023

it won't work. 🤔

$ zig run main.zig
parsed schema: http
parsed host: example.com
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>400 - Bad Request</title>
	</head>
	<body>
		<h1>400 - Bad Request</h1>
	</body>
</html>
Method: GET
Status code: bad_request
Version: HTTP/1.0

@musaprg
Copy link
Author

musaprg commented Mar 5, 2023

$ zig version
0.11.0-dev.1862+e7f128c20

@musaprg
Copy link
Author

musaprg commented Mar 11, 2023

@musaprg
Copy link
Author

musaprg commented Mar 11, 2023

http://example.com

$ zig run main.zig
parsed schema: http
parsed host: example.com
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>400 - Bad Request</title>
	</head>
	<body>
		<h1>400 - Bad Request</h1>
	</body>
</html>
Method: GET
Status code: bad_request
Version: HTTP/1.0

http://example.com/

$ zig run main.zig
parsed schema: http
parsed host: example.com
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
Method: GET
Status code: ok
Version: HTTP/1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment