Hi,
The batch request which i posted, will create a document with "helloworld.txt" text file as primary content. No further actions needed . If there is other file type needed to be added as primary content, then you need to write some code to serialize the file. Below is the code snippet with okhttp client . The code will create a document in windchill with a excel file as primary content.
okhttp3.MultipartBody.Builder batchBuilder =
new okhttp3.MultipartBody.Builder("batch").setType(okhttp3.MultipartBody.MIXED);
// Changeset part
okhttp3.MultipartBody.Builder changesetBuilder =
new okhttp3.MultipartBody.Builder("changeset").setType(okhttp3.MultipartBody.MIXED);
// Changeset part
okhttp3.MultipartBody.Builder contenttBuilder =
new okhttp3.MultipartBody.Builder("ContentSeparator").setType(okhttp3.MultipartBody.FORM);
// First part of changeset
changesetBuilder.addPart(
Headers.of(
"Content-ID", "1",
"Content-Transfer-Encoding", "binary"),
RequestBody.create(
"POST Documents HTTP/1.1\n"
+ "Content-Type: application/json\n\n"
+ "{\n"
+ " \"Name\": \"Created Through PostMan\",\n"
+ " \"@odata.type\": \"PTC.DocMgmt.MyDocSubtype\",\n"
+ " \"Context@odata.bind\": \"Containers('OR:wt.pdmlink.PDMLinkProduct:XXXXXX')\",\n"
+ " \"Folder@odata.bind\": \"Folders('OR:wt.folder.SubFolder:XXXXXX')\",\n"
+ " \"Attr1\": {\n"
+ " \"Value\": \"EN\"\n"
+ " },\n"
+ " \"Attr2\": {\n"
+ " \"Value\": \"1\"\n"
+ " },\n"
+ " \"Attr3\": \"10\"\n"
+ "}\n",
MediaType.parse("application/http")));
// Second part of changeset
changesetBuilder.addPart(
Headers.of(
"Content-ID", "2",
"Content-Transfer-Encoding", "binary"),
RequestBody.create(
"POST $1/PTC.DocMgmt.CheckOut HTTP/1.1\n"
+ "Content-Type: application/json\n\n"
+ "{\n"
+ "\n"
+ "}\n"
+ "\n",
MediaType.parse("application/http")));
MediaType MEDIA_TYPE_EXCEL =
MediaType.parse("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
URI propertyURI =
GenericTest.class
.getClassLoader()
.getResource("PartLoaderTemplate_LoadTesting.xlsx")
.toURI();
String excelpath = Paths.get(propertyURI).toString();
File excelFile = new File(excelpath);
contenttBuilder.addFormDataPart(
"primaryFilepathInput",
excelFile.getName(),
RequestBody.create(MEDIA_TYPE_EXCEL, excelFile));
Buffer rb = new Buffer();
((RequestBody) contenttBuilder.build()).writeTo(rb);
changesetBuilder.addPart(
Headers.of(
"Content-ID", "3",
"Content-Transfer-Encoding", "binary"),
RequestBody.create(
"PUT $2/PrimaryContent HTTP/1.1\n"
+ "Content-Type: multipart/form-data; boundary=ContentSeparator\n\n"
+ rb.readUtf8(),
MediaType.parse("application/http")));
// Fourth part of changeset
changesetBuilder.addPart(
Headers.of(
"Content-ID", "4",
"Content-Transfer-Encoding", "binary"),
RequestBody.create(
"POST $2/PTC.DocMgmt.CheckIn HTTP/1.1\n"
+ "Content-Type: application/json\n\n"
+ "{\n"
+ "\n"
+ "}\n"
+ "\n",
MediaType.parse("application/http")));
// Add changeset to batch
batchBuilder.addPart(changesetBuilder.build());
Request request =
new Request.Builder()
.url("https://XXXXXXX/Windchill/servlet/odata/v5/DocMgmt/$batch")
.post(batchBuilder.build())
.header("CSRF_NONCE", "XXXXXXXXXX")
.addHeader("Content-Type", "multipart/mixed;boundary=batch")
.addHeader("accept", "application/json;odata.metadata=none")
.addHeader(
"Authorization",
"Basic "
+ Base64.getEncoder()
.encodeToString(
("USERNAME" + ":" + "PASSWORD").getBytes(StandardCharsets.UTF_8)))
.build();
try (Response response = client.newCall(request).execute()) {
// Buffer b = new Buffer();
// response.request().body().writeTo(b);
// System.out.println("Req Body: " + b.readUtf8());
assertAll(() -> assertTrue(response.isSuccessful()), () -> assertNotNull(response.body()));
String responseBody = response.body().string();
LOGGER.trace("responseBody = {}", responseBody);
Pattern pattern = Pattern.compile("boundary=(.+)");
Matcher matcher = pattern.matcher(responseBody);
String boundry = "";
while (matcher.find()) {
boundry = matcher.group(1);
}
LOGGER.trace("boundry = {}", boundry);
}