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.

Example:

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="http://www.sap.com/abapxml" version="1.0">
    <asx:values>
    <CAR>
    <MODEL_NAME>Golf New</MODEL_NAME>
    <MANU_ID>1</MANU_ID>
    <PRICE>18599.98</PRICE>
    <CURRENCY>EUR</CURRENCY>
    <MODEL_YEAR>2012</MODEL_YEAR>
     </CAR>
    </asx:values>
    </asx:abap>
    
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 ).
        return.
      endif.
    
      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 ).
        return.
      endif.
    
      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 ).
        return.
      endif.
    
      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 ).
        return.
      endif.
      mo_response->set_etag( ls_etag ).
      mo_response->set_status( cl_rest_status_code=>gc_success_created ).
    
    endmethod.
    
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.