Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

REST PROGRAMMING EXAMPLE R_CAR

Stephen3
Participant
0 Kudos

Hi


I am trying to build this example in my SAP ECC 6.0 EHP7 NW 7.4 system but the table R_CAR doesn't exist in my system.

http://help.sap.com/saphelp_nw74/helpdata/en/0f/5fb77942744afe94afafa78df57b70/content.htm?frameset=...

Question:

1. How do I get r_car in my system

2. How/where do I define s_car type

Thanks

Stephen

2 REPLIES 2

Stephen3
Participant
0 Kudos

Hi Again;


I am hoping someone can help me.  I have built the example listed in this link:

http://help.sap.com/saphelp_nw74/helpdata/en/8e/267cb0f4694dbf8fbaf7eb1cd7c91b/content.htm?frameset=...

I am now trying to build the program "CREATING THE REST CLIENT".  First off I think there are some errors in the help documentation which is causing me problem.  Here is my program:

In the small code example, I have changed the line mo_rest_client->set_header_field to mo_rest_client->set_request_header

Also, the main problem I am having is the line mo_rest_client->post( lo_rest_entity ) is not calling my post routine in the class ZCL_REST_SAMPLE_CARS that I have created even though I have created with a superclass as CL_REST_RESOURCE and have defined it as my resource in IF_REST_APPLICATION~GET_ROOT_HANDLER as specified in the help documentation.

i.e. lo_handler->attach( iv_template = '/Cars' iv_handler_class = 'ZCL_REST_SAMPLE_CARS' ).

I have also make the SICF entry correctly as the program runs but doesn't call the correct post routine.

REPORT ZTEST_REST.

  data: lo_http_client type ref to if_http_client,

           mo_rest_client type ref to if_rest_client,

           mv_csrf_token type string.

  data: lo_rest_client type ref to if_rest_client,

           lo_rest_entity type ref to if_rest_entity,

           lv_return_code type I.

  data: lv_uri_path    type string,

           lv_str             type string,

           lv_xstr           type xstring.

  lv_uri_path = '/sap/bc/rest/cars/car/1'.

  cl_http_client=>create_by_url(

    exporting

      url                   = 'http://myserver:8000/sap/bc/rest/cars/'

    importing

      client               = lo_http_client

   exceptions

      argument_not_found = 1

      plugin_not_active      = 2

      internal_error             = 3

      others                        = 4.

  if sy-subrc <> 0.

    RETURN.

  endif.

  lo_http_client->authenticate( username = '<username>' password = '<password>' ).

  lo_http_client->propertytype_accept_cookie = if_http_client=>co_enabled.

  lo_http_client->request->set_header_field( name = if_http_form_fields_sap=>sap_client value = '700' ).

  create object mo_rest_client type cl_rest_http_client

     exporting

       io_http_client = lo_http_client.

   mo_rest_client->set_request_header(

      iv_name = if_rest_request=>gc_header_csrf_token

      iv_value  = 'Fetch' ).

* NEXT LINE IS COMMENTED OUT AS IT DOESN'T IN MY SETUP.  SHOULD IT OR HAVE I DONE SOMETHING WRONG?

* Commented out from SAP online help example documentation

*  mv_csrf_token = mo_rest_client->get_header_field( name = if_rest_request=>gc_header_csrf_token ).

* ABOVE LINE CHANGED TO:

  mv_csrf_token = mo_rest_client->get_response_header( iv_name = if_reest_request=>gc_header_csrf_token ).

* Create REST Entity + fill in data

  lo_rest_entity = mo_rest_client->create_request_entity( ).

  lv_str = '<?xml version="1.0" encoding="utf-8"?>.

  lv_str = lv_str && '<asx:abap xmlns:asx=http://www.sap.com/abapxml version="1.0">'.

  lv_str = lv_str && '<asx:values>'.

  lv_str = lv_str && '<CAR>'.

  lv_str = lv_str && '<MODEL_NAME>Golf New</MODEL_NAME>'.

  lv_str = lv_str && '<MANU_ID>1</MANU_ID>'.

  lv_str = lv_str && '<PRICE>18599.98</PRICE>'.

  lv_str = lv_str && '<CURRENCY>EUR</CURRENCY>'.

  lv_str = lv_str && '<MODEL_YEAR>2012</MODEL_YEAR>'.

  lv_str = lv_str && '</CAR>'.

  lv_str = lv_str && '</asx:values>'.

  lv_str = lv_str && '</asx:abap>'.

  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

    EXPORTING

      text      = lv_str

    IMPORTING

      buffer = lv_xstr.

* POST TEST

  lo_rest_entity->set_binary_data( lv_xstr ).

  mo_rest_client->post( lo_rest_entity ).

  lv_return_code = mo_rest_client->get_status( ).

  lv_rest_entity = mo_rest_client->get_response_entity( ).

Stephen3
Participant
0 Kudos

Hi All

I finally figured out what was going wrong with my program.  I needed to make a "get" call first and set the CSRF_TOKEN value.  Then, when I made the POST call, I needed to feed in the CSRF_TOKEN value returned from the first get call. This is required so the SAP knows that it is working with the same session.  Otherwise a 403 invalid response is returned which I guess is correct. Using debug mode in Eclipse for ABAP allowed me to debug what was going on on the "server" side.  Any suggestions for improvement are welcome.

REPORT ZTEST_REST.   data: lo_http_client type ref to if_http_client,         mo_rest_client type ref to if_rest_client,         mv_csrf_token  type string.   data: lo_rest_client type ref to if_rest_client,         lo_rest_entity type ref to if_rest_entity,         lv_return_code type i.   data: lv_uri_path    type string,         lv_str         type string,         lv_xstr        type xstring.   lv_uri_path = '/sap/bc/rest_cars/Car/1/'.   cl_http_client=>create_by_url(     exporting       url                = 'http://server.xxx.com:8000/sap/bc/rest_cars/Car/1'     importing       client             = lo_http_client     exceptions       argument_not_found = 1       plugin_not_active  = 2       internal_error     = 3       others             = 4 ).   if sy-subrc <> 0.     RETURN.   endif.   lo_http_client->authenticate( username = 'xxxxx' password = 'xxxxx' ).   lo_http_client->propertytype_accept_cookie = if_http_client=>co_enabled.   lo_http_client->request->set_header_field( name  = if_http_form_fields_sap=>sap_client value = 'X00' ).   create object mo_rest_client type cl_rest_http_client        exporting           io_http_client = lo_http_client. * Commented out from SAP online help example documentation *  mo_rest_client->set_header_field( *      name  = if_rest_request=>gc_header_csrf_token *      value = 'fetch' ). * Above lines changed to   mo_rest_client->set_request_header(       iv_name  = if_rest_request=>gc_header_csrf_token       iv_value = 'fetch' ). ** Commented out from SAP online help example documentation *  mv_csrf_token = mo_rest_client->get_header_field( name = if_rest_request=>gc_header_csrf_token ). * Above lines changed to:   mv_csrf_token = mo_rest_client->get_response_header( iv_name = if_rest_request=>gc_header_csrf_token ).   mo_rest_client->get( ).   lv_return_code = mo_rest_client->get_status( ).   lo_rest_entity = mo_rest_client->get_response_entity( ).   mv_csrf_token = mo_rest_client->get_response_header( iv_name = if_rest_request=>gc_header_csrf_token ).   mo_rest_client->set_request_header(       iv_name  = IF_HTTP_HEADER_FIELDS_SAP=>REQUEST_URI       iv_value = '/sap/bc/rest_cars/Car/1' ).   mo_rest_client->set_request_header(       iv_name  = 'X-CSRF-Token'       iv_value = mv_csrf_token ). * create REST entity + fill in data   lo_rest_entity = mo_rest_client->create_request_entity( ). * Needs to look like this from documentation *

* * * * Golf New* 1* 18599.98* EUR* 2012* * *   lv_str = '

'.   lv_str = lv_str && ''.   lv_str = lv_str && ''.   lv_str = lv_str && ''.   lv_str = lv_str && 'Golf New'.   lv_str = lv_str && '1'.   lv_str = lv_str && '18599.98'.   lv_str = lv_str && 'EUR'.   lv_str = lv_str && '2012'.   lv_str = lv_str && ''.   lv_str = lv_str && ''.   lv_str = lv_str && ''.   CALL FUNCTION 'SCMS_STRING_TO_XSTRING'     EXPORTING       text          = lv_str    IMPORTING      buffer         = lv_xstr. * POST TEST   lo_rest_entity->set_binary_data( lv_xstr ). *  lo_rest_entity->set_string_data( lv_str ).   mo_rest_client->post( lo_rest_entity ).   lv_return_code = mo_rest_client->get_status( ).   lo_rest_entity = mo_rest_client->get_response_entity( ).   * on success, i.e. lv_return_code=201, read new data from REST entity