Sample code for 30+ languages & platforms
Java

effectconnect Product Update

See more effectconnect Examples

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

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Xml;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

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

        Http http = new Http();
        HttpRequest req = new HttpRequest();

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

        // Get the current date/time in timestamp format.
        DateTime dt = new DateTime();
        dt.setFromCurrentSystemTime();
        String timestamp = dt.getAsTimestamp(true);

        req.addHeader("TIME", timestamp);
        System.out.println("timestamp = " + timestamp);

        StringBuilder sbXml = new StringBuilder();
        sbXml.loadFile("qa_data/xml/effectconnect/effconUpdate.xml", "utf-8");
        System.out.println("length = " + sbXml.getLength());

        req.setHttpVerb("PUT");
        req.setPath(uri);
        req.setContentType("multipart/form-data");
        try {
            req.addStringForUpload("payload", "effconUpdate.xml", sbXml.getAsString(), "utf-8");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Build a string-to-sign and sign it using our effectconnect private key
        StringBuilder sbStringToSign = new StringBuilder();
        sbStringToSign.appendInt(sbXml.getLength());
        sbStringToSign.append("PUT");
        sbStringToSign.append(uri);
        sbStringToSign.append(apiVersion);
        sbStringToSign.append(timestamp);

        Crypt2 crypt = new Crypt2();
        crypt.setMacAlgorithm("hmac");
        crypt.setHashAlgorithm("sha512");
        crypt.setEncodingMode("base64");
        // Use your effectconnect private key here:
        crypt.setMacKeyString("PRIVATE_KEY");
        req.addHeader("SIGNATURE", crypt.macStringENC(sbStringToSign.getAsString()));

        HttpResponse resp = new HttpResponse();
        try {
            http.httpSReq("submit.effectconnect.com", 443, true, req, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("response status code = " + resp.getStatusCode());

        // 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.
        Xml xmlResp = new Xml();
        xmlResp.loadXml(resp.getBodyStr());

        System.out.println("response body:");
        System.out.println(xmlResp.getXml());

        // 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>
        //

        String RequestType = xmlResp.getChildContent("Request|RequestType");
        String RequestAction = xmlResp.getChildContent("Request|RequestAction");
        String RequestVersion = xmlResp.getChildContent("Request|RequestVersion");
        String ProcessedAt = xmlResp.getChildContent("Request|ProcessedAt");
        String Result = xmlResp.getChildContent("Response|Result");
        String ProcessID = xmlResp.getChildContent("Response|ProductsUpdateResponseContainer|ProcessID");
    }
}