Sample code for 30+ languages & platforms
React Native

effectconnect Product Update

See more effectconnect Examples

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

Chilkat React Native Downloads

React Native
import { CkDateTime, Crypt2, Http, HttpRequest, HttpResponse, StringBuilder, Xml } from '@chilkat/react-native'

async function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const uri = '/products';
  const apiVersion = '2.0';

  const http = new Http();
  const 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.
  const dt = new CkDateTime();
  dt.setFromCurrentSystemTime();
  const timestamp = dt.getAsTimestamp(true);

  req.addHeader('TIME', timestamp);
  console.log(`timestamp = ${timestamp}`);

  const sbXml = new StringBuilder();
  sbXml.loadFile('qa_data/xml/effectconnect/effconUpdate.xml', 'utf-8');
  console.log(`length = ${sbXml.length}`);

  req.httpVerb = 'PUT';
  req.path = uri;
  req.contentType = 'multipart/form-data';
  try {
    req.addStringForUpload('payload', 'effconUpdate.xml', sbXml.getAsString(), 'utf-8');
  } catch {
    console.log(req.lastErrorText);
    return;
  }

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

  const crypt = new Crypt2();
  crypt.macAlgorithm = 'hmac';
  crypt.hashAlgorithm = 'sha512';
  crypt.encodingMode = 'base64';
  // Use your effectconnect private key here:
  crypt.setMacKeyString('PRIVATE_KEY');
  req.addHeader('SIGNATURE', crypt.macStringENC(sbStringToSign.getAsString()));

  const resp = new HttpResponse();
  try {
    await http.httpSReqAsync('submit.effectconnect.com', 443, true, req, resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  console.log(`response status code = ${resp.statusCode}`);

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

  console.log('response body:');
  console.log(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>
  // 

  const tagPath = '';

  const requestType = xmlResp.getChildContent('Request|RequestType');
  const requestAction = xmlResp.getChildContent('Request|RequestAction');
  const requestVersion = xmlResp.getChildContent('Request|RequestVersion');
  const processedAt = xmlResp.getChildContent('Request|ProcessedAt');
  const result = xmlResp.getChildContent('Response|Result');
  const processID = xmlResp.getChildContent('Response|ProductsUpdateResponseContainer|ProcessID');
}