Sample code for 30+ languages & platforms
Zig

MWS RequestReport (Amazon Marketplace Web Service)

See more Amazon MWS Examples

Creates a report request and submits the request to Amazon MWS.

See Amazon MWS RequestReport for more information.

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.

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect to the Amazon MWS REST server.
    //
    // Make sure to connect to the correct Amazon MWS Endpoint, otherwise
    // you'll get an HTTP 401 response code.
    //
    // The possible servers are:
    //
    // North America (NA)     https://mws.amazonservices.com
    // Europe (EU)     https://mws-eu.amazonservices.com
    // India (IN)     https://mws.amazonservices.in
    // China (CN)     https://mws.amazonservices.com.cn
    // Japan (JP)     https://mws.amazonservices.jp
    //
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    rest.connect("mws.amazonservices.com", port, b_tls, b_auto_reconnect) catch {
        std.debug.print("ConnectFailReason: {d}\n", .{rest.getConnectFailReason()});
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    rest.setHost("mws.amazonservices.com");

    rest.addQueryParam("AWSAccessKeyId", "0PB842EXAMPLE7N4ZTR2") catch {};
    rest.addQueryParam("Action", "RequestReport") catch {};
    rest.addQueryParam("EndDate", "2008-06-26T18:12:21") catch {};
    rest.addQueryParam("MWSAuthToken", "amzn.mws.4ea38b7b-f563-7709-4bae-87aeaEXAMPLE") catch {};
    rest.addQueryParam("Marketplace", "ATVPDKIKX0DER") catch {};
    rest.addQueryParam("ReportType", "_GET_MERCHANT_LISTINGS_DATA_") catch {};
    rest.addQueryParam("SellerId", "A1XEXAMPLE5E6") catch {};
    rest.addQueryParam("SignatureMethod", "HmacSHA256") catch {};
    rest.addQueryParam("SignatureVersion", "2") catch {};
    rest.addQueryParam("StartDate", "2009-01-03T18:12:21") catch {};
    rest.addQueryParam("Version", "2009-01-01") catch {};

    // Add the MWS Signature param.  (Also adds the Timestamp parameter using the curent system date/time.)
    // The AddMwsSignature method adds the Timestamp and Signature query params.
    rest.addMwsSignature("POST", "/Reports/2009-01-01", "mws.amazonservices.com", "MWS_SECRET_KEY") catch {};

    const response_xml = rest.fullRequestFormUrlEncoded(alloc, "POST", "/Reports/2009-01-01") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    if (rest.getResponseStatusCode() != 200) {
        // 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: {s}\n", .{response_xml});
        std.debug.print("---\n", .{});
        std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
        std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
    }

    // Examine the XML returned in the response body.
    std.debug.print("{s}\n", .{response_xml});
    std.debug.print("----\n", .{});
    std.debug.print("Success.\n", .{});

    // Sample Response

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

    // <?xml version="1.0"?>
    // <RequestReportResponse
    //     xmlns="http://mws.amazonaws.com/doc/2009-01-01/">
    //     <RequestReportResult>
    //         <ReportRequestInfo>
    //             <ReportRequestId>2291326454</ReportRequestId>
    //             <ReportType>_GET_MERCHANT_LISTINGS_DATA_</ReportType>
    //             <StartDate>2009-01-21T02:10:39+00:00</StartDate>
    //             <EndDate>2009-02-13T02:10:39+00:00</EndDate>
    //             <Scheduled>false</Scheduled>
    //             <SubmittedDate>2009-02-20T02:10:39+00:00</SubmittedDate>
    //             <ReportProcessingStatus>_SUBMITTED_</ReportProcessingStatus>
    //         </ReportRequestInfo>
    //     </RequestReportResult>
    //     <ResponseMetadata>
    //         <RequestId>88faca76-b600-46d2-b53c-0c8c4533e43a</RequestId>
    //     </ResponseMetadata>
    // </RequestReportResponse>

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadXml(response_xml) catch {};

    _ = try xml.getAttrValue(alloc, "xmlns");
    _ = try xml.getChildContent(alloc, "RequestReportResult|ReportRequestInfo|ReportRequestId");
    _ = try xml.getChildContent(alloc, "RequestReportResult|ReportRequestInfo|ReportType");
    _ = try xml.getChildContent(alloc, "RequestReportResult|ReportRequestInfo|StartDate");
    _ = try xml.getChildContent(alloc, "RequestReportResult|ReportRequestInfo|EndDate");
    _ = try xml.getChildContent(alloc, "RequestReportResult|ReportRequestInfo|Scheduled");
    _ = try xml.getChildContent(alloc, "RequestReportResult|ReportRequestInfo|SubmittedDate");
    _ = try xml.getChildContent(alloc, "RequestReportResult|ReportRequestInfo|ReportProcessingStatus");
    _ = try xml.getChildContent(alloc, "ResponseMetadata|RequestId");
}