Zig Cookbook

Introduction

POST

Parses the supplied URL and makes a synchronous HTTP POST request with request. Prints obtained Response status, and data received from server.

Note: Since HTTP support is in early stage, it’s recommended to use libcurl for any complex task.

const std = @import("std");
const print = std.debug.print;
const http = std.http;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var client = http.Client{ .allocator = allocator };
    defer client.deinit();

    const uri = try std.Uri.parse("https://httpbin.org/anything");

    var req = try client.request(.POST, uri, .{
        .extra_headers = &.{.{ .name = "Content-Type", .value = "application/json" }},
    });
    defer req.deinit();

    var payload: [7]u8 = "[1,2,3]".*;
    try req.sendBodyComplete(&payload);
    var buf: [1024]u8 = undefined;
    var response = try req.receiveHead(&buf);

    // Occasionally, httpbin might time out, so we disregard cases
    // where the response status is not okay.
    if (response.head.status != .ok) {
        return;
    }

    const body = try response.reader(&.{}).allocRemaining(allocator, .unlimited);
    defer allocator.free(body);
    print("Body:\n{s}\n", .{body});
}

Previous: GET
Next: http.Server - std