Monday, July 9, 2018

SRVE0216E: post body contains less bytes than specified by content-length

i was writing service handle HTTP POST request and come accross below exception




00000070 SRTServletReq E com.ibm.ws.webcontainer.srt.SRTServletRequest parseParameters SRVE0133E: An error occurred while parsing parameters. {0}
                                 java.io.IOException: SRVE0216E: post body contains less bytes than specified by content-length
    at com.ibm.ws.webcontainer.servlet.RequestUtils.parsePostData(RequestUtils.java:311)



Using data : JSON.stringify(json) , in the AJAX call resolved issue

Sunday, July 8, 2018

REST POST CALL with JSON BODY in Websphere commerce

REST POST CALL with JSON BODY in Websphere commerce. The code below is an example of a POST request that requests and receives a JSON string response. The Apache Wink framework also supports GET and other applicable request types, but for the purposes of this example a POST request will be leveraged.
Apache Wink REST Client Creation Tutorial for WebSphere Commerce
Step 1. Create a ClientConfig object. This object will be used to configure and construct a RestClient.
By default, the client uses the java.net.HttpURLConnection for issuing requests and responses; but the Apache HttpClient can also be used. In my experience with WebSphere Commerce v7.0, I recommend using the ApacheHttpClientConfig object as shown below to construct your ClientConfig object.
Configure and set appropriate properties on the ClientConfig object, such as the readTimeout and connectTimeout properties.
NOTE: After a ClientConfig object is used to construct a RestClient object, the ClientConfig object can no longer be modified and will result in a ClientConfigException error.

// import org.apache.wink.client.ApacheHttpClientConfig;
// import org.apache.wink.client.ClientConfig;
ClientConfig clientConfig = newApacheHttpClientConfig();
clientConfig.readTimeout(readTimeout);
clientConfig.connectTimeout(connectionTimeout);

Step 2. Create a RestClient with the ClientConfig object created and configured previously.

// import org.apache.wink.client.RestClient;
RestClient restClient = newRestClient(clientConfig);

Step 3. Create a Resource object with your REST service URI from the REST client object. Configure and set appropriate properties on the Resource object, and add any required headers to the request. Ensure you set the appropriate MediaType you expect to receive in response to your REST service call.

// import org.apache.wink.client.Resource;
// import javax.ws.rs.core.MediaType;
Resource restResource = restClient.resource("http://www.example.com/some/rest/service");
restResource.accept(MediaType.APPLICATION_JSON);
resource.header(ECConstants.EC_CONTENT_TYPE, MediaType.APPLICATION_JSON);
resource.header("Authorization","<Basic cmFjaGVsLmZvb0BmYWly>");
restResource.header("HeaderField", "HeaderValue");

Step 4. Creating JSON object to send in body for POST call

String json=null;
Map detailsMap = new LinkedHashMap<String, String>();
    detailsMap.put("ticket",detailsMap);
                    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
                    try {
                    json = ow.writeValueAsString(detailsMap);
System.out.println("request proprs :::: "+json);
            } catch (JsonProcessingException e) {
            e.printStackTrace();
}

Step 5. Execute a GET or POST call, and process the response by using the status code, response headers, or the response message body as needed.

// import org.apache.wink.client.ClientResponse;
ClientResponse clientResponse = restResource. .contentType(MediaType.APPLICATION_JSON).post(json.toString());
int statusCode = clientResponse.getStatusCode();
String responseEntity = clientRepsonse.getEntity(String.class);
            }


NOTE: Instead of calling the response.getEntity(String.class) to retrieve a String response, you can use any other class that has a valid javax.ws.rs.ext.MessageBodyReader object.
And there you go, you’ve implemented an Apache Wink REST service client, and can now process the response as your application requires.