Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 3.31 KB | None | 0 0
  1. *&---------------------------------------------------------------------*
  2. *& Report  Z_RESTHTTP_COMMUNICATION1
  3. *&
  4. *&---------------------------------------------------------------------*
  5. *&
  6. *&
  7. *&---------------------------------------------------------------------*
  8.  
  9. REPORT Z_RESTHTTP_COMMUNICATION1.
  10.  
  11. TYPES: BEGIN OF json_str,
  12.   id              TYPE string,
  13.   employee_name   TYPE string,
  14.   employee_salary TYPE string,
  15.   employee_age    TYPE string,
  16.   profile_image   TYPE string,
  17. END OF json_str.
  18.  
  19.  
  20.  
  21. * Declarations
  22. DATA lo_client         TYPE REF TO if_http_client.
  23. DATA lo_conv           TYPE REF TO cl_abap_conv_in_ce.
  24. DATA lo_rest_client    TYPE REF TO cl_rest_http_client.
  25. DATA lo_response       TYPE REF TO if_rest_entity.
  26.  
  27. DATA lv_result_url     TYPE string.
  28. DATA lv_url            TYPE string.
  29. DATA lv_json           TYPE string.
  30. DATA lv_response       TYPE string.
  31. DATA lv_httpcode       TYPE i.
  32. DATA lv_reason         TYPE string.
  33. DATA lv_message        TYPE string.
  34. DATA lv_index          TYPE i.
  35. DATA lt_data_json      TYPE json_str. "Tabela na odpowiedz JSON
  36. DATA response          TYPE string.
  37.  
  38.  
  39. "Z_REST_TEST utworzony w zarządzaniu połączeniami RFC w transakcji SM59
  40. CALL METHOD cl_http_client=>create_by_destination
  41.   EXPORTING
  42.     destination              = 'Z_REST_TEST'
  43.   IMPORTING
  44.     client                   = lo_client
  45.   EXCEPTIONS
  46.     argument_not_found       = 1
  47.     destination_not_found    = 2
  48.     destination_no_authority = 3
  49.     plugin_not_active        = 4
  50.     internal_error           = 5
  51.     OTHERS                   = 6.
  52. IF sy-subrc <> 0.
  53.  
  54. ENDIF.
  55.  
  56. IF lo_client IS BOUND.
  57.   lo_client->request->set_method( if_http_request=>co_request_method_get ).
  58. *
  59. * Construct the URL
  60.   CONCATENATE '?id='
  61.               '&employee_name='
  62.               '&employee_salary='
  63.               '&employee_age='
  64.               '&profile_image='
  65.               INTO lv_result_url.
  66.  
  67. * Url
  68.   cl_http_utility=>set_request_uri( request = lo_client->request uri = lv_result_url ).
  69.  
  70. * Send the HTTP request
  71.   CALL METHOD lo_client->send
  72.     EXCEPTIONS
  73.       http_communication_failure = 1
  74.       http_invalid_state         = 2
  75.       http_processing_failed     = 3
  76.       http_invalid_timeout       = 4
  77.       OTHERS                     = 5.
  78.   IF sy-subrc <> 0.
  79.  
  80.   ENDIF.
  81.  
  82. * HTTP call receive
  83.   CALL METHOD lo_client->receive
  84.     EXCEPTIONS
  85.       http_communication_failure = 1
  86.       http_invalid_state         = 2
  87.       http_processing_failed     = 3
  88.       OTHERS                     = 4.
  89.   IF sy-subrc <> 0.
  90.  
  91.   ENDIF.
  92.  
  93. *   get status of the response
  94.   CALL METHOD lo_client->response->get_status
  95.     IMPORTING
  96.       code   = lv_httpcode
  97.       reason = lv_reason.
  98.  
  99. * Instantiate REST client
  100.   CREATE OBJECT lo_rest_client
  101.     EXPORTING
  102.       io_http_client = lo_client.
  103.  
  104.  
  105.   lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
  106.  
  107. * Json zwrócony zapytaniem get , do stringa
  108.   response = lo_response->get_string_data( ).
  109.  
  110. "Konwersja bedzie dzialac po utworzeniu struktury odpowiedzi JSON
  111.   CALL METHOD cl_fdt_json=>json_to_data
  112.     EXPORTING
  113.       iv_json = response
  114.     CHANGING
  115.       ca_data = lt_data_json.
  116.  
  117.   CALL METHOD lo_client->close
  118.     EXCEPTIONS
  119.       http_invalid_state = 1
  120.       OTHERS             = 2.
  121.   IF sy-subrc <> 0.
  122.  
  123.   ENDIF.
  124.  
  125. ENDIF.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement