Windchill REST Services > Windchill REST Services Framework Capabilities > Processing Batch Requests > Examples for Batch Requests
  
Examples for Batch Requests
This section provides examples for batch requests
Batch Request for Creating Parts
The following is a batch request with change sets which creates parts.
POST /Windchill/servlet/odata/ProdMgmt/$batch HTTP/1.1
CSRF_NONCE: <Use the value from Fetch NONCE example>
Content-Type: multipart/mixed;boundary=batch_request


--batch_request
Content-Type:multipart/mixed;boundary=changeset_abc

--changeset_abc
Content-Type: application/http
Content-Transfer-Encoding:binary
Content-Id: 1

POST /Windchill/servlet/odata/ProdMgmt/Parts HTTP/1.1
Content-Type: application/json

{
"Name":"BatchPart1",
"Context@odata.bind": "Containers('OR:wt.pdmlink.PDMLinkProduct:76625')"
}

--changeset_abc
Content-Type: application/http
Content-Transfer-Encoding:binary
Content-Id: 2

POST /Windchill/servlet/odata/ProdMgmt/Parts HTTP/1.1
Content-Type: application/json

{
"Name":"BatchPart2",
"Context@odata.bind": "Containers('OR:wt.pdmlink.PDMLinkProduct:76625')"
}
--changeset_abc--

--batch_request--
The response to the batch request is as below:
--batch_3487ef0a-9598-41cb-a3cf-1d8616ed9e58
Content-Type: multipart/mixed; boundary=changeset_2f39c1fb-0e4b-4a60-9e2d-89cab451db7f

--changeset_2f39c1fb-0e4b-4a60-9e2d-89cab451db7f
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

HTTP/1.1 201 Created
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal
Content-Length: 1149

{"@odata.context":"$metadata#Parts","ID":"OR:wt.part.WTPart:601168","Name":"BatchPart1","Number":"0000019103","EndItem":false,"TypeIcon":{"Path":"https://windchill.ptc.com/Windchill/wtcore/images/part.gif","Tooltip":"Part"},"Identity":"0000019103, BatchPart1, Demo Organization, A","GeneralStatus":null,"ShareStatus":null,"ChangeStatus":null,"Supersedes":null,"AssemblyMode":{"Value":"separable","Display":"Separable"},"DefaultUnit":{"Value":"ea","Display":"each"},"DefaultTraceCode":{"Value":"0","Display":"Untraced"},"Source":{"Value":"make","Display":"Make"},"ConfigurableModule":{"Value":"standard","Display":"No"},"GatheringPart":false,"PhantomManufacturingPart":false,"BOMType":null,"AlternateNumber":null,"View":"","CheckoutState":"Checked in","CheckOutStatus":"","Comments":null,"State":{"Value":"INWORK","Display":"In Work"},"LifeCycleTemplateName":"Basic","VersionID":"VR:wt.part.WTPart:601167","Revision":"A","Version":"A.0","Latest":true,"FolderName":null,"CabinetName":"Default","FolderLocation":"/Default","OrganizationName":"Demo Organization","CreatedOn":"2010-04-20T08:32:51Z","LastModified":"2010-04-20T08:32:51Z"}
--changeset_2f39c1fb-0e4b-4a60-9e2d-89cab451db7f
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2

HTTP/1.1 201 Created
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal
Content-Length: 1149

{"@odata.context":"$metadata#Parts","ID":"OR:wt.part.WTPart:601176","Name":"BatchPart2","Number":"0000019104","EndItem":false,"TypeIcon":{"Path":"https://windchill.ptc.com/Windchill/wtcore/images/part.gif","Tooltip":"Part"},"Identity":"0000019104, BatchPart2, Demo Organization, A","GeneralStatus":null,"ShareStatus":null,"ChangeStatus":null,"Supersedes":null,"AssemblyMode":{"Value":"separable","Display":"Separable"},"DefaultUnit":{"Value":"ea","Display":"each"},"DefaultTraceCode":{"Value":"0","Display":"Untraced"},"Source":{"Value":"make","Display":"Make"},"ConfigurableModule":{"Value":"standard","Display":"No"},"GatheringPart":false,"PhantomManufacturingPart":false,"BOMType":null,"AlternateNumber":null,"View":"","CheckoutState":"Checked in","CheckOutStatus":"","Comments":null,"State":{"Value":"INWORK","Display":"In Work"},"LifeCycleTemplateName":"Basic","VersionID":"VR:wt.part.WTPart:601175","Revision":"A","Version":"A.0","Latest":true,"FolderName":null,"CabinetName":"Default","FolderLocation":"/Default","OrganizationName":"Demo Organization","CreatedOn":"2010-04-20T08:32:51Z","LastModified":"2010-04-20T08:32:51Z"}
--changeset_2f39c1fb-0e4b-4a60-9e2d-89cab451db7f--
--batch_3487ef0a-9598-41cb-a3cf-1d8616ed9e58--
Batch Request with References Between Entity and Property Value
In the following example, there are five changesets with following content ID:
Content-ID: 1_1—Creates a part with the name PARENT.
Content-ID: 2_1—Creates a part with the name CHILD.
Content-ID: 3_1—References part PARENT from Content-ID: 1_1 and checks out the part.
Content-ID: 4_1—References the checked out part PARENT from Content-ID: 3_1. Creates bind relationship with CHILD, which is referenced from Content-ID: 2_1.
Content-ID: 5_1—References the checked out part PARENT from Content-ID: 3_1 and checks in the part.
--batch_1
Content-Type: multipart/mixed;boundary=changeset_1


--changeset_1
Content-Id: 1_1
Content-Transfer-Encoding: binary
Content-Type: application/http

POST /Windchill/servlet/odata/v2/ProdMgmt/Parts HTTP/1.1
Accept: application/json
Content-Type: application/json

{"Context@odata.bind":"Containers('OR:wt.pdmlink.PDMLinkProduct:71864')","AssemblyMode":{"Value":"separable"},"DefaultUnit":{"Value":"ea"},"GatheringPart":false,"DefaultTraceCode":{"Value":"0"},"PhantomManufacturingPart":false,"Source":{"Value":"make"},"Name":"PARENT"}

--changeset_1--

--batch_1
Content-Type: multipart/mixed;boundary=changeset_2


--changeset_2
Content-Id: 2_1
Content-Transfer-Encoding: binary
Content-Type: application/http

POST /Windchill/servlet/odata/v2/ProdMgmt/Parts HTTP/1.1
Accept: application/json
Content-Type: application/json

{"Context@odata.bind":"Containers('OR:wt.pdmlink.PDMLinkProduct:71864')","AssemblyMode":{"Value":"separable"},"DefaultUnit":{"Value":"ea"},"GatheringPart":false,"DefaultTraceCode":{"Value":"0"},"PhantomManufacturingPart":false, "Source":{"Value":"make"},"Name":"CHILD"}

--changeset_2--

--batch_1
Content-Type: multipart/mixed;boundary=changeset_3


--changeset_3
Content-Id: 3_1
Content-Transfer-Encoding: binary
Content-Type: application/http

POST $1_1/PTC.ProdMgmt.CheckOut HTTP/1.1
Accept: application/json
Content-Type: application/json

{}

--changeset_3--

--batch_1
Content-Type: multipart/mixed;boundary=changeset_4


--changeset_4
Content-Id: 4_1
Content-Transfer-Encoding: binary
Content-Type: application/http

POST $3_1/Uses HTTP/1.1
Accept: application/json
Content-Type: application/json

{"TraceCode":{"Value":"L"},"FindNumber":"F001","Quantity":2,"Uses@odata.bind":"$2_1","Unit":{"Value":"ea"},"LineNumber":100}

--changeset_4--

--batch_1
Content-Type: multipart/mixed;boundary=changeset_5


--changeset_5
Content-Id: 5_1
Content-Transfer-Encoding: binary
Content-Type: application/http

POST $3_1/PTC.ProdMgmt.CheckIn HTTP/1.1
Accept: application/json
Content-Type: application/json

{}

--changeset_5--

--batch_1--
Batch Request for creating and uploading content to a document
Refer the following examples:
1. To create a document, check it out and then upload content to the checked out document, all as part of a batch, is as follows:
ContentType: multipart/mixed;boundary=batch
--batch
Content-Type: multipart/mixed;boundary=changeset

--changeset
Content-ID: 1
Content-Type: application/http
Content-Transfer-Encoding:binary

POST Documents HTTP/1.1
Content-Type: application/json

{

"Name":"BT002",

"Description":"Document Batch Test 002",

"Title":"Document TD002",

"Context@odata.bind": "Containers('OR:wt.pdmlink.PDMLinkProduct:95801')"

}

--changeset
Content-Type: application/http
Content-ID: 2
Content-Transfer-Encoding:binary

POST $1/PTC.DocMgmt.CheckOut HTTP/1.1
Content-Type: application/json

{

}

--changeset
Content-Type: application/http
Content-ID: 3
Content-Transfer-Encoding:binary

PUT $2/PrimaryContent HTTP/1.1
Content-Type: multipart/form-data; boundary=ContentSeparator

--ContentSeparator
Content-Disposition: form-data; name="primaryFilepathInput";filename="helloworld.txt"
Content-Type: text/plain

Hello, World!

--ContentSeparator--

--changeset
Content-Type: application/http
Content-ID: 4
Content-Transfer-Encoding:binary

POST $2/PTC.DocMgmt.CheckIn HTTP/1.1
Content-Type: application/json

{

}

--changeset--

--batch--
2. To upload a binary file, the file content needs to be serialized and sent as part of the request body:
--changeset
Content-Type: application/http
Content-ID: 3
Content-Transfer-Encoding:binary

PUT $2/PrimaryContent HTTP/1.1
Content-Type: multipart/form-data; boundary=ContentSeparator

--ContentSeparator
Content-Disposition: form-data; name="primaryFilepathInput";filename="myPDF_file.pdf"
Content-Type: application/pdf
Content-Transfer-Encoding: binary

<serialized_content>

--ContentSeparator--
If using Java, the following code snippet shows how to write a file to HttpURLConnection:
java.net.URL urlBatchObj = new URL("<Windchill_URL>/servlet/odata/v6/DocMgmt/$batch");
java.net.HttpURLConnection urlBatchCon = (HttpURLConnection) urlBatchObj.openConnection();
urlBatchCon.setRequestMethod("POST");
urlBatchCon.setRequestProperty("Content-Type", "multipart/mixed;boundary=batch");
urlBatchCon.setRequestProperty("CSRF_NONCE", csrf_nonce);
urlBatchCon.setDoInput(true);
urlBatchCon.setDoOutput(true);

java.io.File fi = new File("path/to/file");

String requestBodyFirstPart = "<your_batch_request_body_begin>" +
"PUT $001/PrimaryContent HTTP/1.1\r\n" +
"Content-Type: multipart/form-data; boundary=attachmentBoundary\r\n" +
"\r\n" +
"--attachmentBoundary\r\n"+
"Content-Disposition: form-data;name=\"file1\"; filename=\"your_file.pdf\"\r\n" +
"Content-Type: application/pdf\r\n"+
"Content-Transfer-Encoding: binary\r\n"+
"\r\n";

String requestBodyEnd = "<your_batch_request_body_end>";

java.io.OutputStream outputStream = urlBatchCon.getOutputStream();
java.io.BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));

writer.append(requestBodyFirstPart);
writer.flush();
java.nio.file.Files.copy(fi.toPath(), outputStream);
outputStream.flush();
writer.append(requestBodyEnd);
writer.flush();
outputStream.flush();
writer.close();
outputStream.close();

int responseCode = urlBatchCon.getResponseCode();