Show TOC

Example documentationThe PUT Method Locate this document in the navigation structure

 

The method IF_REST_RESOURCE~PUT is used to update a single entity of type Car identified by the client-specified URI.

Example:

The URI http://myhost:4711/sap/bc/rest_cars/Car/2 is a URI identifying the Car resource with ID=2. This URI triggers together with the usage of the PUT command in the HTTP request the calling of method IF_REST_RESOURCE~PUT.

A valid HTTP body looks like this:

Syntax Syntax

  1. <?xml version="1.0" encoding="utf-8"?>
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
    <asx:values>
    <CAR>
    <MODEL_NAME>Zafira</MODEL_NAME>
    <MANU_ID>3</MANU_ID>
    <PRICE>20599.98</PRICE>
    <CURRENCY>EUR</CURRENCY>
    <MODEL_YEAR>2012</MODEL_YEAR>
    </CAR>
    </asx:values>
    </asx:abap>
    
End of the code.

In addition, a valid ETag value has to be provided by HTTP header If-Match. The value for this HTTP header (for example, W/"datetime'2012-10-05T13%3A25%3A14.3910880'") is read by a REST client, for example, using a previous GET on the same resource URI. This ensures that the previously read data was not changed in the meantime.

Syntax Syntax

  1. method if_rest_resource~put.
    ...
      lv_id = mo_request->get_uri_attribute( iv_name = 'ID' ).
    
      select single * from r_car
        into ls_db_car
        where id = lv_id.
      if sy-subrc <> 0.
        mo_response->set_status( cl_rest_status_code=>gc_client_error_not_found ).
        return.
      endif.
    
      get time stamp field lv_updated.      " get timestamp for new ETag value
      lo_entity = mo_request->get_entity( ).
      lv_xstr = lo_entity->get_binary_data( ).
    
      call transformation id
        source xml lv_xstr
        result car = ls_db_car_in.
      if ls_db_car_in is initial.
        mo_response->set_status( cl_rest_status_code=>gc_client_error_bad_request ).
        return.
      endif.
    
      ls_db_car_in-id = lv_id.
      ls_db_car_in-update_timestamp = lv_updated.
      modify r_car from ls_db_car_in.
      if sy-subrc <> 0.
        mo_response->set_status( cl_rest_status_code=>gc_server_error_internal ).
        return.
      endif.
    
      ls_etag-weak = abap_true.
      ls_etag-tag = get_etag( lv_updated ).
      if ls_etag-tag is initial.
        rollback work.
        mo_response->set_status( cl_rest_status_code=>gc_server_error_internal ).
        return.
      endif.
    
      mo_response->set_etag( ls_etag ).
      mo_response->set_status( cl_rest_status_code=>gc_success_no_content ).
    
    endmethod.
    
End of the code.
  • The URI element representing the key of the resource is assigned by the REST library to the parameter named ID (this is identical to GET method example) and read by the request object's method GET_URI_ATTRIBUTE.

  • The read in ID is used to select the requested data.

  • The data provided in the request object MO_REQUEST is deserialized (simply by the ABAP ID-transformation), the database update takes place and a new ETag header is set in the response.

  • As in the GET example, the member object MO_RESPONSE is already created by the library before the PUT method is called.

  • In the PUT case, there is no response data set in the HTTP body, but only the HTTP response code 201 ("No content") is set.