Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *&---------------------------------------------------------------------*
- *& Report Z_RESTHTTP_COMMUNICATION1
- *&
- *&---------------------------------------------------------------------*
- *&
- *&
- *&---------------------------------------------------------------------*
- REPORT Z_RESTHTTP_COMMUNICATION1.
- TYPES: BEGIN OF json_str,
- id TYPE string,
- employee_name TYPE string,
- employee_salary TYPE string,
- employee_age TYPE string,
- profile_image TYPE string,
- END OF json_str.
- * Declarations
- DATA lo_client TYPE REF TO if_http_client.
- DATA lo_conv TYPE REF TO cl_abap_conv_in_ce.
- DATA lo_rest_client TYPE REF TO cl_rest_http_client.
- DATA lo_response TYPE REF TO if_rest_entity.
- DATA lv_result_url TYPE string.
- DATA lv_url TYPE string.
- DATA lv_json TYPE string.
- DATA lv_response TYPE string.
- DATA lv_httpcode TYPE i.
- DATA lv_reason TYPE string.
- DATA lv_message TYPE string.
- DATA lv_index TYPE i.
- DATA lt_data_json TYPE json_str. "Tabela na odpowiedz JSON
- DATA response TYPE string.
- "Z_REST_TEST utworzony w zarządzaniu połączeniami RFC w transakcji SM59
- CALL METHOD cl_http_client=>create_by_destination
- EXPORTING
- destination = 'Z_REST_TEST'
- IMPORTING
- client = lo_client
- EXCEPTIONS
- argument_not_found = 1
- destination_not_found = 2
- destination_no_authority = 3
- plugin_not_active = 4
- internal_error = 5
- OTHERS = 6.
- IF sy-subrc <> 0.
- ENDIF.
- IF lo_client IS BOUND.
- lo_client->request->set_method( if_http_request=>co_request_method_get ).
- *
- * Construct the URL
- CONCATENATE '?id='
- '&employee_name='
- '&employee_salary='
- '&employee_age='
- '&profile_image='
- INTO lv_result_url.
- * Url
- cl_http_utility=>set_request_uri( request = lo_client->request uri = lv_result_url ).
- * Send the HTTP request
- CALL METHOD lo_client->send
- EXCEPTIONS
- http_communication_failure = 1
- http_invalid_state = 2
- http_processing_failed = 3
- http_invalid_timeout = 4
- OTHERS = 5.
- IF sy-subrc <> 0.
- ENDIF.
- * HTTP call receive
- CALL METHOD lo_client->receive
- EXCEPTIONS
- http_communication_failure = 1
- http_invalid_state = 2
- http_processing_failed = 3
- OTHERS = 4.
- IF sy-subrc <> 0.
- ENDIF.
- * get status of the response
- CALL METHOD lo_client->response->get_status
- IMPORTING
- code = lv_httpcode
- reason = lv_reason.
- * Instantiate REST client
- CREATE OBJECT lo_rest_client
- EXPORTING
- io_http_client = lo_client.
- lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
- * Json zwrócony zapytaniem get , do stringa
- response = lo_response->get_string_data( ).
- "Konwersja bedzie dzialac po utworzeniu struktury odpowiedzi JSON
- CALL METHOD cl_fdt_json=>json_to_data
- EXPORTING
- iv_json = response
- CHANGING
- ca_data = lt_data_json.
- CALL METHOD lo_client->close
- EXCEPTIONS
- http_invalid_state = 1
- OTHERS = 2.
- IF sy-subrc <> 0.
- ENDIF.
- ENDIF.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement