Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.1.0+

Creating an application/json HTTP POST Request

See more HTTP Examples

Demonstrates how to create an HTTP POST request having the Content-Type application/json, where the body of the HTTP request is the following JSON:
{
   "username" : "my_username",
   "password" : "my_password",
   "validation-factors" : {
      "validationFactors" : [
         {
            "name" : "remote_address",
            "value" : "127.0.0.1"
         }
      ]
   }
}

The generated HTTP request looks like this:

POST /something HTTP/1.1
Content-Type: application/json
Accept: application/json
Host: domain
Content-Length: 216

{
  "username": "my_username",
  "password": "my_password",
  "validation-factors": {
    "validationFactors": [
      {
        "name": "remote_address",
        "value": "127.0.0.1"
      }
    ]
  }
}

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 demonstrates building an application/json request.

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();

    // The ContentType, HttpVerb, and Path properties should
    // always be explicitly set.
    req.setHttpVerb("POST");
    req.setPath("/something");
    req.setContentType("application/json");

    // We may wish to add an "Accept" header to the request.
    // This tells the server we'll accept an application/json response.
    req.addHeader("Accept", "application/json");

    // We'll use Chilkat's JSON API to create the JSON body of the HTTP request.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.appendString("username", "my_username") catch {};
    json.appendString("password", "my_password") catch {};

    const v_factors = try chilkat.JsonObject.init();
    defer v_factors.deinit();
    json.appendObject2("validation-factors", v_factors) catch {};

    const v_array = try chilkat.JsonArray.init();
    defer v_array.deinit();
    v_factors.appendArray2("validationFactors", v_array) catch {};

    const factor_obj = try chilkat.JsonObject.init();
    defer factor_obj.deinit();
    v_array.addObjectAt2(0, factor_obj) catch {};
    factor_obj.appendString("name", "remote_address") catch {};
    factor_obj.appendString("value", "127.0.0.1") catch {};

    // Use the JSON for the HTTP request body
    // By default, the Emit method will output compact JSON.
    // This is best for generating the smallest size request.
    // To generate a more human-readable (pretty-printed) JSON request body,
    // set the EmitCompact property to FALSE
    json.setEmitCompact(false);
    req.loadBodyFromString(try json.emit(alloc), "utf-8") catch {};

    // View the request that would be sent if HttpSReq was called:
    const request_mime = try req.generateRequestText(alloc);
    std.debug.print("{s}\n", .{request_mime});

    // A few important comments about the HTTP request that is generated:
    //
    // 1) The Content-Length header is automatically generated based on the actual length of the MIME message
    //    that follows the intial (topmost) MIME header.
    // 2) The HOST header will automatically get filled in with the actual domain when HttpSReq
    //    is called.
}