How to create a case in TGB-HRApps-Work-Candidate using Pega API cases
Hi all,
I'm writing a Java program and want to create a case in TGB-HRApps-Work-Candidate (Pega 7 self study exercise environment). I tried some JSON combinations like:
{
"caseTypeID": "TGB-HRApps-Work-Candidate",
"processID": "pyStartCase",
"parentCaseID": "",
"content": {}
}
But all combination result in a HTTP 400 code with error:
{"pxObjClass":"Pega-API-CaseManagement-Case","errors":[ {"ID":"Pega_API_001","message":"Invalid request data","pxObjClass":"Pega-API-Error"}] }
Using the Pega API page from Developer Studio the above JSON works fine creating a new case and a HTTP 201 code!
Any idea what I'm doing wrong?
Code:
Hi all,
I'm writing a Java program and want to create a case in TGB-HRApps-Work-Candidate (Pega 7 self study exercise environment). I tried some JSON combinations like:
{
"caseTypeID": "TGB-HRApps-Work-Candidate",
"processID": "pyStartCase",
"parentCaseID": "",
"content": {}
}
But all combination result in a HTTP 400 code with error:
{"pxObjClass":"Pega-API-CaseManagement-Case","errors":[ {"ID":"Pega_API_001","message":"Invalid request data","pxObjClass":"Pega-API-Error"}] }
Using the Pega API page from Developer Studio the above JSON works fine creating a new case and a HTTP 201 code!
Any idea what I'm doing wrong?
Code:
package nl.ing.pega;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainPost {
private static String payload = "{ \"caseTypeID\": \"TGB-HRApps-Work-Candidate\", \"processID\": \"pyStartCase\", \"parentCaseID\": \"\", \"content\": {} }";
private static HttpURLConnection connection = null;
public static void main(String[] args) {
String requestUrl="http:// Proprietary information hidden:9080/prweb/api/v1/cases";
System.out.println("Request:\r\n" + payload);
System.out.println();
sendGetRequest(requestUrl, payload);
}
public static void sendGetRequest(String requestUrl, String payload) {
try {
URL url = new URL(requestUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", Util.buildBasicAuthorizationString());
// connection.setRequestProperty("Content-Type", "application/json");
// connection.setRequestProperty("Content-Length", Integer.toString(payload.length()));
System.out.println("HTTP code: " + connection.getResponseCode());
System.out.println();
if(connection.getResponseCode() == 201) {
System.out.println("Response:\r\n" + Util.readResponse(connection));
}
} catch (Exception e) {
System.err.println("Exception:\r\n");
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
if(connection!=null) {
try {
if(connection.getResponseCode() == 400 ||
connection.getResponseCode() == 403) {
Thread.sleep(500);
System.out.print("Error response:\r\n");
System.out.println(Util.readResponse(connection, true));
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
Util.responseHeaders(connection);
connection.disconnect();
}
}
}
}
package nl.ing.pega;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;
public class Util {
public static String readResponse(HttpURLConnection connection) throws Exception {
return readResponse(connection, false);
}
public static String readResponse(HttpURLConnection connection, boolean isError) throws Exception {
BufferedReader br = null;
if(isError) {
br = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
} else {
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
StringBuffer jsonString = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect();
return jsonString.toString();
}
public static String buildBasicAuthorizationString() {
String credentials = "admin@tgb:rules";
return "Basic " + new String(Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8)));
}
public static void responseHeaders(HttpURLConnection connection) {
System.out.println("\r\nResponse header:");
Map<String, List<String>> map = connection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
Result:
"C:\Program Files\Java\jdk1.8.0_152\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.1\lib\idea_rt.jar=56692:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_152\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar;C:\Projects\IntellIJ\PegaAPICall\out\production\PegaAPICall" nl.ing.pega.MainPost
Request:
{ "caseTypeID": "TGB-HRApps-Work-Candidate", "processID": "pyStartCase", "parentCaseID": "", "content": {} }
HTTP code: 400
Error response:
{"pxObjClass":"Pega-API-CaseManagement-Case","errors":[ {"ID":"Pega_API_001","message":"Invalid request data","pxObjClass":"Pega-API-Error"}] }
Response header:
null : [HTTP/1.1 400 Bad Request]
Warning : [299 Proprietary information hidden "Use of TLS/SSL is strongly recommended for services using Basic Authentication"]
Server : [Apache-Coyote/1.1]
Connection : [close]
Set-Cookie : [JSESSIONID=48A3CB19C6CEDF31171D1E5743165DF1; Path=/prweb; HttpOnly]
Content-Length : [153]
Date : [Sat, 09 Mar 2019 17:03:05 GMT]
Content-Type : [application/json;charset=UTF-8]
Process finished with exit code 0
Thnx.
Regards,
Oene