Skip to Content

The following Blog is about modifying an entity within an SAP Netweaver Gateway OData Service.

The classical way for updating data

Assuming we have a resource in the backend and we wish to modify it, we typically proceed as follows:

  1. First, execute a GET request in order to READ the details of an entity.
    In our example, we invoke the following URL, in order to READ the details of a “product”:
    http://<server>:<port>/sap/opu/odata/IWBEP/GWDEMO/ProductCollection(‘005056A503AC1ED28FC00CB008856ACC’)
    Our response body looks like follows:
    READ_details_new.jpg
  2. In a  second step:
    – copy the relevant content into the request body of a REST client
    – change the value of the property we wish to modify
    – send it as PUT request back to the same URL

    In our example, we wish to modify only the “description” of the “product”.
    We do so and send the complete xml, including all unchanged properties, to the backend using the HTTP-verb PUT

    The following screenshot shows such a PUT request (using the Gateway Client)

    PUT_product.jpg

Note:

In this Blog, we use the SAP Netweaver Gateway Client (transaction /IWFND/GW_CLIENT) for sending HTTP requests.

One of the many advantages is the button “Use as Request”: it copies the response body to the request body pane.

Let’s hesitate a second:

We wonder why we have to send all that stuff if only one property is to be changed…?

The reason is the specification of the HTTP-Verb PUT:

A PUT request indicates a replacement update.

The Gateway Service must replace all property values with those specified in the request body.

Missing properties must be set to their default values.

Missing dynamic properties must be removed or set to NULL.

The simple way

Now, we’d like to introduce a new feature, which comes with SAP Netweaver Gateway 2.0 SP05:

The support for the PATCH operation.

A PATCH request indicates a different update.

The Gateway Service must replace exactly those property values that are specified in the request body.

Missing properties, including dynamic properties, must not be altered.

Note: the HTTP verb PATCH is treated equivalently to MERGE

Now we repeat our example, this time using a PATCH instead of a PUT request.

  1. The first step is identical:
    – execute a GET request in order to READ the details of an entity.
  2. The second step:
    – we copy only that property we wish to modify from the response to the request body
    – change the value
    – send it as PATCH request  

    In our example, the PATCH request looks as follows:

    PATCH_1.jpg

That’s all.

Just to verify that with a PATCH operation also multiple properties can be modified, have a look at the following screenshot:

PATCH_2.jpg

Note about service Implementation

For those of you who are implementing Gateway Services in ABAP:

PATCH support is provided with method PATCH_ENTITY of the interface /IWBEP/IF_MGW_APPL_SRV_RUNTIME.

The Gateway Framework provides a basic default implementation of the PATCH_ENTITY method. Prerequisites however are implemented “read” and “update” methods.

The default implementation performs a read before update in order to fetch the complete entity.

The properties of this entity are merged with the property/properties provided within the PATCH request.

At the end, an update_entity is performed.

This default implementation has the side-effect that there’s some overhead, which results in some time loss.  However, these read-before-update calls are done inside the Backend-System (BEP), so no additional communication between Hub and Backend is done. The performance loss is so small that it can be ignored.

On the other hand, the service-creator has the opportunity to increase the performance by providing a custom implementation of this method.

The API Reference can be found in the SAP help:

Developer Guide -> OData Channel -> Advanced Features -> Patch Support

Direct link: http://help.sap.com/saphelp_gateway20sp05/helpdata/en/81/ec4c1e138c4cb09c8b392aab893365/frameset.htm

Note about performance

The duration of a PUT versus a PATCH request can be analyzed and compared using the SAP Netweaver Tracing Tool, which can be started with transaction /IWFND/TRACES.

Find more information about the Trace Tool in the official SAP documentation:

Technical Operations Guide -> Supportability -> Performance Trace

http://help.sap.com/saphelp_gateway20sp05/helpdata/en/9d/da3a2ceca344cf85568ae927e9858d/frameset.htmhttp://help.sap.com/saphelp_gateway20sp05/helpdata/en/56/d0cc05b564411e841141f68294e29f/frameset.htm

With regard to the HTTP communication, the data which is transferred from the client to the Gateway server is much slimmer, which disburdens the load of the HTTP communication.

On the Gateway-side, there’s some overhead of merging the patched properties with the unchanged properties of the entity, before an update is performed.

Thus, we don’t have to wonder that the overall performance of a PATCH is slightly worse than a PUT.

The advantage of the PATCH is the easy handling on the client-side and the thin load of HTTP communication.

The picture below shows a comparison between a PATCH (lines 1 to 12) and a PUT request.

We can see that the duration of the PATCH (883 ms) is slightly higher than the duration of the PUT (819 ms)

We can as well see that– within the PATCH stack – the read and update calls are highlighted in dark blue, which means that they are performed on the Backend-side only.

/wp-content/uploads/2013/01/trace_patch_180167.jpg

To report this post you need to login first.

11 Comments

You must be Logged on to comment or reply to a post.

  1. Sreehari V Pillai

    I am getting this as response.. please help me on this..

    <?xml version=”1.0″ encoding=”utf-8″ ?>

    <error xmlns=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata>

    <code>CX_ST_MATCH_ELEMENT/544FAE4641562346A1372144E7396586</code>

    <message xml:lang=”en>System expected the element ‘{http://www.w3.org/2005/Atom}entry’</message>

    <innererror>

    <transactionid>B6C63BE388FEF159876A001517AAF695</transactionid>

    <errordetails />

    </innererror>

    </error>

    XML passed in the body.

       <?xml version=“1.0” encoding=“utf-8” ?>
      <feed xml:base=http://****/sap/opu/odata/sap/ZMAKT_DESC_SRV/xmlns=http://www.w3.org/2005/Atomxmlns:m=http://schemas.microsoft.com/ado/2007/08/dataservices/metadataxmlns:d=http://schemas.microsoft.com/ado/2007/08/dataservices>
       <id>http://***/sap/opu/odata/sap/ZMAKT_DESC_SRV/Makt</id>
       <title type=“text”>Makt</title>
       <updated>2013-10-23T09:40:08Z</updated>
      <author>
       <name />
       </author>
       <link href=“Makt” rel=“self” title=“Makt” />
      <entry>
       <id>http://****/sap/opu/odata/sap/ZMAKT_DESC_SRV/Makt(Spras=‘E’,Matnr=‘1’)</id>
       <title type=“text”>Makt(Spras=‘E’,Matnr=’10’)</title>
       <updated>2013-10-23T09:40:08Z</updated>
       <category term=“ZMAKT_DESC_SRV.Makt” scheme=http://schemas.microsoft.com/ado/2007/08/dataservices/scheme/>
       <link href=“Makt(Spras=’E’,Matnr=’10’)” rel=“edit” title=“Makt” />
      <content type=“application/xml”>
      <m:properties>
       <d:Maktg>Changed text data</d:Maktg>
       <d:Maktx>Changed text data</d:Maktx>
       <d:Spras>E</d:Spras>
       <d:Matnr>10</d:Matnr>
       </m:properties>
       </content>
       </entry>
       </feed>

    I I implemented GEt_ENTITY, GET_ENTITYSET,UPDATE_ENTITY.



    (0) 
    1. Carlos Roggan Post author

      Hi, as I can see from your xml, it seems that you’ve copied the response body of a QUERY.

      The outer tags of your xml are <feed>

      The error message says that the system expected an <entry> element.

      As desribed above, we do a READ operation.

      This is an HTTP GET request to a single <entry>

      Then use the same url and change the operation to PUT or PATCH

      In your xml you can see that the feed is a collection of zero or many entries. In your xml you can see that the feed contains one entry element.

      The entry element contains a tag <Id> and there you can see the URL for the READ operation:

      http://****/sap/opu/odata/sap/ZMAKT_DESC_SRV/Makt(Spras=‘E’,Matnr=‘1’)

      You have to invoke this url and then use the response body as base for the request body of the PUT or PATCH operation.

      Kind Regards,

      Carlos

      (0) 
      1. Sreehari V Pillai

        Hi Carlos,

        Thanks a lot for the help. Now, i am able to UPDATE(Both PUT and PATCH), CREATE and READ . Is there any method available to update multiple records in batch ?

        Thanks in advance .

        (0) 
        1. Carlos Roggan Post author

          Hi Sreehari,

          yes, the OData specification supports the batch processing via the $batch parameter.

          You can find the official documentation here:

          http://help.sap.com/saphelp_gateway20sp07/helpdata/en/90/dc8363306c47d3b2fca1398f5de94b/content.htm

          Or navigate to it as follows:

          help.sap.com -> SAP Netweaver -> Gateway -> Developer Guide –ö> OData Chanel -> Advanced Features -> $batch Processing

          And there’s a Blog in SCN as well:

          Batch Request in Netweaver Gateway(Multiple Operations into a Single Request)

          Quick info:

          The URL is the service-base-URL with the suffix /$batch

          In your example: http://***/sap/opu/odata/sap/ZMAKT_DESC_SRV/$batch

          The verb is POST

          Add the HEADER: Content-Type    with value: multipart/mixed;boundary=batch_zmybatch

          The request body contains all the desired single requests in a special notation

          Example for a request body:

          –batch_zmybatch

          Content-Type: multipart/mixed; boundary=changeset_zmychangeset1

          –changeset_zmychangeset1

          Content-Type: application/http

          Content-Transfer-Encoding: binary

          PUT <YourCollectionName(‘11111’) HTTP/1.1

          Content-Type: application/atom+xml

          Content-Length: 1021

          <atom:entry xmlns:atom=”http://www.w3.org/2005/Atom“>

          <atom:content type=”application/xml”>

          <m:properties xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices“>

             <d:prop1>value1</d:prop1>

             <d:prop2>value2</d:prop2>

          </m:properties>

          </atom:content>

          </atom:entry>

          –changeset_zmychangeset1–

          –batch_zmybatch

          Content-Type: multipart/mixed; boundary=changeset_zmychangeset2

          –changeset_zmychangeset2

          Content-Type: application/http

          Content-Transfer-Encoding: binary

          PUT <YourCollectionName(‘22222’) HTTP/1.1

          Content-Type: application/atom+xml

          Content-Length: 1021

          <atom:entry xmlns:atom=”http://www.w3.org/2005/Atom“>

          <atom:content type=”application/xml”>

          <m:properties xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices“>

             <d:prop1>value1</d:prop1>

             <d:prop2>value2</d:prop2>

          </m:properties>

          </atom:content>

          </atom:entry>

          –changeset_zmychangeset2–

          –batch_zmybatch

          Content-Type: application/http

          Content-Transfer-Encoding: binary

          GET <YourCollectionName(‘11111’) HTTP/1.1

          –batch_zmybatch

          Content-Type: application/http

          Content-Transfer-Encoding: binary

          GET <YourCollectionName(‘22222’) HTTP/1.1

          –batch_zmybatch–

          (0) 
  2. Eric Zhao

    Hi, where and how can I find my input property (ProductDexcription) in method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~PATCH_ENTITY.1.PNG

    (0) 
    1. Volker Drees

      Hello Eric,

      this can be achieved using the io_data_provider, e.g.:

        io_data_provider->read_entry_data(

          IMPORTING

            es_data = <ls_patched_entity>

        ).

      The list of provided properties can be determined by:

        lt_components = io_tech_request_context->get_components( ).

      I’d recommend that you take a look at the default implementation of /IWBEP/IF_MGW_APPL_SRV_RUNTIME~PATCH_ENTITY  in class /IWBEP/CL_MGW_ABS_DATA.

      Best Regards, Volker

      (0) 
  3. Thiru Siva

    There are many times where the caller uses just PUT and inadvertently overwrites data. So it is always better that a PATCH is performed.

    (0) 

Leave a Reply