Sample code for 30+ languages & platforms
Unicode C

effectconnect Product Update

See more effectconnect Examples

Use this call to update a product (f.e. stock or price) in EffectConnect.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkDateTimeW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkCrypt2W.h>
#include <C_CkHttpResponseW.h>
#include <C_CkXmlW.h>

void ChilkatSample(void)
    {
    BOOL success;
    const wchar_t *uri;
    const wchar_t *apiVersion;
    HCkHttpW http;
    HCkHttpRequestW req;
    HCkDateTimeW dt;
    const wchar_t *timestamp;
    HCkStringBuilderW sbXml;
    HCkStringBuilderW sbStringToSign;
    HCkCrypt2W crypt;
    HCkHttpResponseW resp;
    HCkXmlW xmlResp;
    const wchar_t *tagPath;
    const wchar_t *RequestType;
    const wchar_t *RequestAction;
    const wchar_t *RequestVersion;
    const wchar_t *ProcessedAt;
    const wchar_t *Result;
    const wchar_t *ProcessID;

    success = FALSE;

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

    uri = L"/products";
    apiVersion = L"2.0";

    http = CkHttpW_Create();
    req = CkHttpRequestW_Create();

    // Use your effectconnect public key here...
    CkHttpRequestW_AddHeader(req,L"KEY",L"PUBLIC_KEY");
    CkHttpRequestW_AddHeader(req,L"VERSION",apiVersion);
    CkHttpRequestW_AddHeader(req,L"URI",uri);
    CkHttpRequestW_AddHeader(req,L"RESPONSETYPE",L"XML");
    CkHttpRequestW_AddHeader(req,L"RESPONSELANGUAGE",L"en");

    // Get the current date/time in timestamp format.
    dt = CkDateTimeW_Create();
    CkDateTimeW_SetFromCurrentSystemTime(dt);
    timestamp = CkDateTimeW_getAsTimestamp(dt,TRUE);

    CkHttpRequestW_AddHeader(req,L"TIME",timestamp);
    wprintf(L"timestamp = %s\n",timestamp);

    sbXml = CkStringBuilderW_Create();
    success = CkStringBuilderW_LoadFile(sbXml,L"qa_data/xml/effectconnect/effconUpdate.xml",L"utf-8");
    wprintf(L"length = %d\n",CkStringBuilderW_getLength(sbXml));

    CkHttpRequestW_putHttpVerb(req,L"PUT");
    CkHttpRequestW_putPath(req,uri);
    CkHttpRequestW_putContentType(req,L"multipart/form-data");
    success = CkHttpRequestW_AddStringForUpload(req,L"payload",L"effconUpdate.xml",CkStringBuilderW_getAsString(sbXml),L"utf-8");
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpRequestW_lastErrorText(req));
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        CkDateTimeW_Dispose(dt);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    // Build a string-to-sign and sign it using our effectconnect private key
    sbStringToSign = CkStringBuilderW_Create();
    CkStringBuilderW_AppendInt(sbStringToSign,CkStringBuilderW_getLength(sbXml));
    CkStringBuilderW_Append(sbStringToSign,L"PUT");
    CkStringBuilderW_Append(sbStringToSign,uri);
    CkStringBuilderW_Append(sbStringToSign,apiVersion);
    CkStringBuilderW_Append(sbStringToSign,timestamp);

    crypt = CkCrypt2W_Create();
    CkCrypt2W_putMacAlgorithm(crypt,L"hmac");
    CkCrypt2W_putHashAlgorithm(crypt,L"sha512");
    CkCrypt2W_putEncodingMode(crypt,L"base64");
    // Use your effectconnect private key here:
    CkCrypt2W_SetMacKeyString(crypt,L"PRIVATE_KEY");
    CkHttpRequestW_AddHeader(req,L"SIGNATURE",CkCrypt2W_macStringENC(crypt,CkStringBuilderW_getAsString(sbStringToSign)));

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpSReq(http,L"submit.effectconnect.com",443,TRUE,req,resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        CkDateTimeW_Dispose(dt);
        CkStringBuilderW_Dispose(sbXml);
        CkStringBuilderW_Dispose(sbStringToSign);
        CkCrypt2W_Dispose(crypt);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    wprintf(L"response status code = %d\n",CkHttpResponseW_getStatusCode(resp));

    // Examine the response.  The response status code can be 200 for both errors and success.
    // The success or error is based on the XML returned in the response body.
    xmlResp = CkXmlW_Create();
    CkXmlW_LoadXml(xmlResp,CkHttpResponseW_bodyStr(resp));

    wprintf(L"response body:\n");
    wprintf(L"%s\n",CkXmlW_getXml(xmlResp));

    // A sample response:

    // <?xml version="1.0" encoding="utf-8"?>
    // <ApiResponseContainer>
    //     <Request>
    //         <RequestType>Products</RequestType>
    //         <RequestAction>Update</RequestAction>
    //         <RequestVersion>2.0</RequestVersion>
    //         <RequestIdentifier/>
    //         <ProcessedAt>2019-04-18T15:37:32+02:00</ProcessedAt>
    //     </Request>
    //     <Response>
    //         <Result>Success</Result>
    //         <ProductsUpdateResponseContainer>
    //             <ProcessID><![CDATA[f81ngzD2S7gooFk3]]></ProcessID>
    //         </ProductsUpdateResponseContainer>
    //     </Response>
    // </ApiResponseContainer>
    // 

    RequestType = CkXmlW_getChildContent(xmlResp,L"Request|RequestType");
    RequestAction = CkXmlW_getChildContent(xmlResp,L"Request|RequestAction");
    RequestVersion = CkXmlW_getChildContent(xmlResp,L"Request|RequestVersion");
    ProcessedAt = CkXmlW_getChildContent(xmlResp,L"Request|ProcessedAt");
    Result = CkXmlW_getChildContent(xmlResp,L"Response|Result");
    ProcessID = CkXmlW_getChildContent(xmlResp,L"Response|ProductsUpdateResponseContainer|ProcessID");


    CkHttpW_Dispose(http);
    CkHttpRequestW_Dispose(req);
    CkDateTimeW_Dispose(dt);
    CkStringBuilderW_Dispose(sbXml);
    CkStringBuilderW_Dispose(sbStringToSign);
    CkCrypt2W_Dispose(crypt);
    CkHttpResponseW_Dispose(resp);
    CkXmlW_Dispose(xmlResp);

    }