Zig
Zig
Explaining the Rest ClearResponseBodyStream Method
See more Azure Cloud Storage Examples
The ClearResponseBodyStream method would be needed if your applicaiton called SetResponseBodyStream to send the response to a stream for one request, but then wants to subsequently do something else. The application must call ClearResponseBodyStream to no longer send responses to the stream.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
var success: bool = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect to the web server
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
success = if (rest.connect("www.chilkatsoft.com", port, b_tls, b_auto_reconnect)) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
}
// Setup a file stream for the download
const file_stream = try chilkat.Stream.init();
defer file_stream.deinit();
file_stream.setSinkFile("qa_output/starfish.jpg");
// Indicate that the call to FullRequestNoBody should send the response body
// to fileStream if the response status code is 200.
// If a non-success response status code is received, then nothing
// is streamed to the output file and the error response is returned by FullRequestNoBody.
const expected_status = 200;
rest.setResponseBodyStream(expected_status, true, file_stream) catch {};
const response_str = rest.fullRequestNoBody(alloc, "GET", "/images/starfish.jpg") catch "";
if (!rest.getLastMethodSuccess()) {
// Examine the request/response to see what happened.
std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("response body (if any): {s}\n", .{response_str});
std.debug.print("---\n", .{});
std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
return;
}
std.debug.print("downloaded starfish.jpg\n", .{});
// Clear the response body stream previously set in the call to SetResponseBodyStream.
rest.clearResponseBodyStream();
// Download something else, but this time send the response body to bd.
const bd = try chilkat.BinData.init();
defer bd.deinit();
success = if (rest.fullRequestNoBodyBd("GET", "/images/penguins.jpg", bd)) true else |_| false;
if (!success) {
// Examine the request/response to see what happened.
std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("response body (if any): {s}\n", .{try bd.getString(alloc, "utf-8")});
std.debug.print("---\n", .{});
std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
return;
}
// Save the contents of bd to a file.
success = if (bd.writeFile("qa_output/penguins.jpg")) true else |_| false;
std.debug.print("Success: {}\n", .{success});
}