Sample code for 30+ languages & platforms
Zig

ETrade Cancel Order

See more ETrade Examples

The cancel order API is used to cancel an existing order.

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 requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    http.setOAuth1(true);
    http.setOAuthVerifier("");
    http.setOAuthConsumerKey("ETRADE_CONSUMER_KEY");
    http.setOAuthConsumerSecret("ETRADE_CONSUMER_SECRET");

    // Load the access token previously obtained via the OAuth1 Authorization
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/etrade.json") catch {
        std.debug.print("Failed to load OAuth1 token\n", .{});
        return;
    };

    http.setOAuthToken(try json_token.stringOf(alloc, "oauth_token"));
    http.setOAuthTokenSecret(try json_token.stringOf(alloc, "oauth_token_secret"));

    const sandbox_url = "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/orders/cancel";

    http.setUrlVar("accountIdKey", "6_Dpy0rmuQ9cu9IbTfvF2A") catch {};

    // Send a PUT with the following XML body

    // Use this online tool to generate the code from sample XML:
    // Generate Code to Create XML

    // <CancelOrderRequest>
    //    <orderId>11</orderId>
    // </CancelOrderRequest>

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("CancelOrderRequest");
    xml.updateChildContent("orderId", "11");
    xml.setEmitCompact(true);

    const http_request_body = try xml.getXml(alloc);
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpStr("PUT", sandbox_url, http_request_body, "utf-8", "application/xml", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Make sure a successful response was received.
    if (resp.getStatusCode() > 200) {
        std.debug.print("{s}\n", .{try resp.getStatusLine(alloc)});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
        return;
    }

    // Sample XML response:

    // Use this online tool to generate parsing code from sample XML:
    // Generate Parsing Code from XML

    // <CancelOrderResponse>
    //    <accountId>63438617</accountId>
    //    <orderId>11</orderId>
    //    <cancelTime>1529563499081</cancelTime>
    //    <Messages>
    //       <Message>
    //          <code>5011</code>
    //          <description>200|Your request to cancel your order is being processed.</description>
    //          <type>WARNING</type>
    //       </Message>
    //    </Messages>
    // </CancelOrderResponse>

    xml.loadXml(try resp.getBodyStr(alloc)) catch {};
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    _ = xml.getChildIntValue("accountId");
    _ = xml.getChildIntValue("orderId");
    _ = try xml.getChildContent(alloc, "cancelTime");
    _ = xml.getChildIntValue("Messages|Message|code");
    _ = try xml.getChildContent(alloc, "Messages|Message|description");
    _ = try xml.getChildContent(alloc, "Messages|Message|type");

    std.debug.print("Success.\n", .{});
}