Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
kenichi_unnai
Advisor
Advisor
12,983

This page is for explaining how to test the OData $batch services with Content ID, which is implemented in the H2G.


In this H2G, we're going through the steps of testing OData $batch services with Content ID by REST Client.


In the test steps, we'll create a $batch request that contains one parent entity with Content ID $100, and two children.

Two testing tools are explained:

SAP Gateway Client


In the SAP tx SEGW, find your Gateway project and select "Service Maintenance" > "Your GW System" > "SAP Gateway Client".

In a SAP Gateway Client tool, enter following request information:


Note: SAP Gateway Client handles all the authentication info such as X-CSRF-Token or Basic Auth behind the scenes.


  • HTTP Method = POST
  • Request URI = the URL has $batch suffix
  • Content-Type = multipart/mixed;boundary=batch_mybatch
  • Payload body = enter the batch script

And execute it - confirm the successful response.

POSTMAN REST client


Start a POSTMAN REST client. Type the URL of your OData service document URL (the URL that ends with "_SRV/"). Set following params:

  • HTTP GET
  • Content-Type = multipart/mixed;boundary=batch_mybatch
  • X-CSRF-Token = Fetch
  • Authentication - (basic auth credentials)

Execute the request by pressing "Send". You should be able to obtain the returned x-csrf-token value. Copy it - this is required to execute HTTP POST later.

Paste the token value for X-CSRF-Token param in the header. Set following params:


  • HTTP POST - the URL has $batch suffix
  • Content-Type = multipart/mixed;boundary=batch_mybatch
  • X-CSRF-Token = <token>
  • Authentication - (basic auth credentials)

Now your POSTMAN is fully ready for sending the $batch payload. Click on "Body" and select raw.

Here you'll enter the payload. (Explanation follows)

01 --batch_mybatch
02 Content-Type: multipart/mixed; boundary=changeset_mychangeset1
03
04 --changeset_mychangeset1
05 Content-Type: application/http
06 Content-Transfer-Encoding: binary
07
08 POST HeaderSet HTTP/1.1
09 Content-Type: application/xml
10 Content-Length: 1021
11 Content-ID: 100
12
13 <?xml version="1.0" encoding="utf-8"?>
14 <entry xml:base="http://..._SRV/"
15     xmlns="http://www.w3.org/2005/Atom"
16     xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
17     xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
18     <content type="application/xml">
19         <m:properties>
20             <d:Id>1</d:Id>
21             <d:Text>New parent entity</d:Text>
22         </m:properties>
23     </content>
24 </entry>
25
26 --changeset_mychangeset1
27 Content-Type: application/http
28 Content-Transfer-Encoding: binary
29
30 POST $100/ToItems HTTP/1.1
31 Content-Type: application/xml
32 Content-Length: 1021
33
34 <?xml version="1.0" encoding="utf-8"?>
35 <entry xml:base="http://..._SRV/"
36     xmlns="http://www.w3.org/2005/Atom"
37     xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
38     xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
39     <content type="application/xml">
40         <m:properties>
41             <d:ParentId></d:ParentId>
42             <d:Id>000010</d:Id>
43             <d:Text>First child entity</d:Text>
44         </m:properties>
45     </content>
46 </entry>
47
48 --changeset_mychangeset1
49 Content-Type: application/http
50 Content-Transfer-Encoding: binary
51
52 POST $100/ToItems HTTP/1.1
53 Content-Type: application/xml
54 Content-Length: 1021
55
56 <?xml version="1.0" encoding="utf-8"?>
57 <entry xml:base="http://..._SRV/"
58     xmlns="http://www.w3.org/2005/Atom"
59     xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
60     xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
61     <content type="application/xml">
62         <m:properties>
63             <d:ParentId></d:ParentId>
64             <d:Id>000020</d:Id>
65             <d:Text>Second child entity</d:Text>
66         </m:properties>
67     </content>
68 </entry>
69
70 --changeset_mychangeset1--
71
72 --batch_mybatch--

#01 - The beginning of the $batch request. It has to start with "--" with the batch name, which is the same name you defined in the header of Content-Type = multipart/mixed;boundary=batch_mybatch.

#02 - Content type definition of the changeset.

#03 - This line space is very important. At least one "carriage return line feed" is required.

#04 - The beginning of the changeset. It has to start with "--" with the changeset name, which is the same name you defined in the line #02.

#05/06 - Conventional value for Content-Type and Content-Transfer-Encoding.

#07 - At least one "carriage return line feed" is required.

#08/09 - POST request for HeaderSet entity.

#10 - Content-Length value must be equal to or greater than the real length of the data below.

#11 - A line of the Content ID value.

#12 - At least one "carriage return line feed" is required.

#13 to 24 - The HeaderSet payload. You can obtain this payload format via Read operation. If you obtain the payload via Read, that should contain the tags like <id/>, <title/>, <updated/>, <category/>, and <link/>. You can keep them in this $batch payload or just discard them - the OData services simply ignore them.

#25 - At least one "carriage return line feed" is required.

#26 - As the next entity is a part of the same changeset, it has the same value with the line #04.

#27 to 29 - Same rule follows in the ItemSet.

#30 - POST request for ItemSet entity. As you see, the Content ID is being used.

#31 to 33 - Same rule follows in the ItemSet.

#34 to 46 - The ItemSet payload. Just like you did for HeaderSet entity, you can obtain this payload format via Read operation. Please note this payload has the empty value for <parentID> tag. During OData $batch processing, this value will be handled correctly via the Content ID value.

#47 to 69 - The second HeaderItem POST request.

#70 - The closing of the changeset. It has to start with "--" with the changeset name AND closing "--" characters.

#71 - At least one "carriage return line feed" is required.

#72 - The closing of the batch request. It has to start with "--" with the batch name AND closing "--" characters.

Let's execute the $batch request by pressing Send button. If the $batch processing is done correctly, you should be able to see "201 Created" responses for each entity in the returned payload body.


Note: As written, the line spacing is critical (it is a part of the standard OData specification).

Time to experiment further... try set the remote breakpoint in the ABAP editor - and see how the changeset_begin, changeset_process, and changeset_end are called sequentially.

13 Comments