Show TOC

Example documentationThe POST Method Locate this document in the navigation structure


The method IF_REST_RESOURCE~POST is used to create a single entity of type Car.


The URI http://myhost:4711/sap/bc/rest_cars/Car with a POST command triggers the call to method IF_REST_RESOURCE~POST in handler class CL_REST_SAMPLE_CARS (see section Attaching ABAP Processor Classes).

The HTTP body of the POST looks like this:

Syntax Syntax

  1. <?xml version="1.0" encoding="utf-8"?>
    <asx:abap xmlns:asx="" version="1.0">
End of the code.

This is the implementation of the POST method for creating a Car entity resource.

Syntax Syntax

  1. method if_rest_resource~post.
      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 ).
      get time stamp field lv_updated.
      ls_db_car_in-update_timestamp = lv_updated.
      select max( id ) from r_car into ls_db_car_in-id.
      add 1 to ls_db_car_in-id.
      insert r_car from ls_db_car_in.
      if sy-subrc <> 0.
        mo_response->set_status( cl_rest_status_code=>gc_server_error_internal ).
      call transformation id
        source dealer = ls_db_car_in
        result xml lv_xstr.
      if sy-subrc <> 0.
        mo_response->set_status( cl_rest_status_code=>gc_server_error_internal ).
      lo_entity = mo_response->create_entity( ).
      lo_entity->set_binary_data( lv_xstr ).
      ls_etag-weak = abap_true.
      ls_etag-tag = get_etag( lv_updated ).
      if ls_etag is initial.
        rollback work.
        mo_response->set_status( cl_rest_status_code=>gc_server_error_internal ).
      mo_response->set_etag( ls_etag ).
      mo_response->set_status( cl_rest_status_code=>gc_success_created ).
End of the code.
  • The URI does not represent a single resource, but is the URI assigned to the collection of resources to create a new one.

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

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

  • The newly created resource data is set in the HTTP body.

  • The response code HTTP 201 ("Created") is set.