Show TOC

Example documentationThe GET Method Locate this document in the navigation structure

 

The method IF_REST_RESOURCE~GET is used to get a single entity of type Car identified by a client specified key.

Example:

The URI http://myhost:4711/sap/bc/rest_cars/Car/2 ends up in calling method IF_REST_RESOURCE~GET using an HTTP GET command. The resource URI template '/Car/{ID:[1-9][0-9]*}' matches and identifies the class CL_REST_SAMPLE_CAR as the handler class for this request (see section Attaching ABAP Processor Classes). The value 2 is filled in the call parameter named ID.

The returned data may look 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>
    <ID>2</ID>
    <MODEL_NAME>Zafira</MODEL_NAME>
    <MANU_ID>3</MANU_ID>
    <PRICE>20599.98</PRICE>
    <CURRENCY>EUR</CURRENCY>
    <MODEL_YEAR>2012</MODEL_YEAR>
    <UPDATE_TIMESTAMP>20121005140643.422526</UPDATE_TIMESTAMP>
     </CAR>
    </asx:values>
    </asx:abap>
    
End of the code.

An Etag header is returned: ETag: W/"datetime'2012-10-05T14%3A06%3A43.4225260'" This is the implementation of the GET method for a Car entity resource.

Syntax Syntax

  1. method if_rest_resource~get.
    ...
      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.
      lv_updated = ls_db_car-update_timestamp.
      call transformation id
        source car = ls_db_car
        result xml lv_xstr.
    
      ls_etag-weak = abap_true.
      ls_etag-tag = get_etag( lv_updated ).
      if ls_etag-tag is initial.
        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_ok ).
    
      lo_entity = mo_response->create_entity( ).
      lo_entity->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_xml ).
      lo_entity->set_binary_data( lv_xstr ).
    
    endmethod.
    
End of the code.
  • During call processing, the REST library wraps the initial HTTP client request to contain a URI attribute named ID. This parameter is read via method GET_URI_ATTRIBUTE) from the wrapped HTTP request object.

  • The read in ID is used to select the data, transformed in a simple XML format (using the ABAP ID transformation) and the ETag header is set in the response object.

  • The response object MO_RESPONSE does not need to be created in the current method, because it is already created by the library before the GET method is called.

Note Note

If you have own serialization operations (and for REST methods requiring input from the client also deserialization-operations) to be performed on application specific data formats, you can use the methods described in section REST HTTP Content Processing by deriving an own serialization/deserialization class from the REST library class CL_REST_ENTITY_PROVIDER to provide the required operations. You find an example for the usage of class CL_REST_HTTP_RESP_PROV (which is a subclass of CL_REST_ENTITY_PROVIDER) for providing operations on a HTTP response object, in section Implementing the Multipart Processor Class. That class formats a REST response of type IF_REST_ENTITY via its WRITE_TO method into an HTTP response.

End of the note.