Sample code for 30+ languages & platforms
Zig

HTTPS Upload File to Web Server

See more HTTP Examples

Uploads a file to a web server using HTTPS.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // The ContentType, HttpVerb, and Path properties should
    // always be explicitly set.
    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.setHttpVerb("POST");
    req.setPath("/receiveMyUpload.aspx");
    req.setContentType("multipart/form-data");

    req.addStringForUpload("fileA", "fileA.txt", "This is the contents of file A", "utf-8") catch {};
    req.addFileForUpload("starfish.jpg", "qa_data/jpg/starfish.jpg") catch {
        std.debug.print("{s}\n", .{try req.getLastErrorText(alloc)});
        return;
    };

    const http = try chilkat.Http.init();
    defer http.deinit();

    // ----------------------------------------------------------------------------
    // IMPORTANT:
    // HTTP uploads require a counterpart implementation on the server, written in any desired language
    // such as C#, Classic ASP, PHP, etc., which consumes the upload being sent.
    // See: ASP.NET Receive Upload
    // ----------------------------------------------------------------------------

    // Do the upload.
    const use_ssl_tls = true;
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpSReq("www.example.com", 443, use_ssl_tls, req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("response status code = {d}\n", .{resp.getStatusCode()});
    std.debug.print("response body:\n", .{});
    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
}