This discussion is archived
1 Reply Latest reply: Feb 18, 2013 3:33 AM by RahulV RSS

Add to Cart via REST Web Service

981180 Newbie
Currently Being Moderated
Hi,
Currently I'm trying to implement e-Commerce via REST Web Services that can be accessed from mobile devices, for which I just went through the Oracle - ATG docs on 'Web Service And Integration Framework' and have done following things

Added REST module to the project
Configured restSecurityConfiguration.xml

And I'm trying to add an item to cart via REST by directly accessing the CartFormHandler as follows
restSecurityConfiguration.xml contains

+<?xml version="1.0" encoding="UTF-8" standalone="no"?>+
+<rest-security>+
+<resource component="/atg/store/order/purchase/CartFormHandler">+
+<method name="addItemToOrder" secure="false" />+
+</resource>+
+</rest-security>+

To add an item to cart basically addItemToOrder method requires productId, catalogRefIds (skuIds), quantity and I'm using ATG REST Java client library to post those parameters from plain Java program as follows

+public static void main(String[] args) throws RestClientException {+

RestSession mSession = RestSession.createSession(“localhost”, “8080”, “username”, “password”);
mSession.setUseHttpsForLogin(false);
+try {+
String loginStatus = mSession.login();
RestResult result = null;
+if (loginStatus == null || "null".equals(loginStatus)) {+
mSession = null;
System.out.println("Login Failed");
+} else {+
System.out.println("Login Successful");
Map<String, Object> map = new HashMap<String, Object>();
String catRefIds[] = new String[1];
catRefIds[0] = "12345";
map.put("productId", "12345");
map.put("catalogRefIds", catRefIds);
map.put("quantity", "2");
result = RestComponentHelper.executeMethod("/atg/store/order/purchase/CartFormHandler", "addItemToOrder", null, map, mSession);
String responseData = result.readInputStream();
+if (responseData != null) {+
JSONObject json = new JSONObject(responseData);
System.out.println(json.toString());
+}+
+}+
+} catch (Throwable t) {+
System.out.println(t);
+} finally {+
+try {+
+if (mSession != null) {+
mSession.logout();
System.out.println("Logout Successful");
+}+
+} catch (RestClientException e) {+
System.out.println(e);
+}+
+}+
+}+

and tried the same thing using the curl as follows,

curl -v -b cookies.txt -X POST -H “Content-Type: application/json” -d “{ 'productId':'12345', 'catalogRefIds':['12345'], 'quantity':'2' }” http://localhost:8080/rest/bean/atg/store/order/purchase/CartFormHandler/addItemToOrder?atg-rest-json-input=true

From server side I can able to get the productId and quantity properly but only the catalogRefIds is getting as empty and the hence ATG throws me the exception as 'Please select a SKU ID to add to the cart', so Can someone point me what I'm actually doing wrong?

Note: I'm using ATG10.0.3
  • 1. Re: Add to Cart via REST Web Service
    RahulV Newbie
    Currently Being Moderated
    Hi,

    In REST we cannot pass arrays like this:

    String catRefIds[] = new String[1];
    catRefIds[0] = "12345";
    map.put("catalogRefIds", catRefIds);

    This will never work.

    Had done a similar implementation. The thing I did was, I wrote a logic that accepts comma separated catalogRefIds and converts it to individual catalogRefId and populate the list.

    Regards,
    Rahul

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points