메뉴 건너뛰기

SAP 한국 커뮤니티

API

sapjoy 2022.11.02 19:30 조회 수 : 310

*&---------------------------------------------------------------------*
*& Report ZAPI
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZAPI.
* Declaración de tipos.
TYPES: BEGIN OF ty_datos,
name1 LIKE lfa1-name1,
sortl LIKE lfa1-sortl,
land1 LIKE lfa1-land1,
spras LIKE lfa1-spras,
END OF ty_datos.

DATA : lo_http_client TYPE REF TO if_http_client.
DATA : l_str_length     TYPE i.
DATA : mime      TYPE w3mimetabtype.
  DATA: l_datos TYPE ty_datos,
        t_datos TYPE STANDARD TABLE OF ty_datos.
  DATA gv_json_output TYPE string.
*SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-s01.
*PARAMETERS: p_inv TYPE belnr_d DEFAULT '12345'.
*SELECTION-SCREEN: END OF BLOCK b1.
START-OF-SELECTION.
*Data variables for storing response in xstring and string
  DATA  : lv_xstring   TYPE xstring,
          lv_string    TYPE string,
          lv_node_name TYPE string.
  CLEAR : lv_xstring, lv_string, lv_node_name.
  DATA  : lv_cdata     TYPE string.
  DATA  : lv_content_length_value TYPE i.
  CLEAR : lv_cdata, lv_content_length_value.
*Pass the URL to get Data
    CLEAR : lv_xstring, lv_string, lv_node_name.
  lv_string = 'https://openapi.naver.com/v1/papago/n2mt?source=ko&target=en&text=안녕.'.
*Creation of New IF_lo_http_client Object
  cl_http_client=>create_by_url(
  EXPORTING
    url                = lv_string
  IMPORTING
    client             = lo_http_client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    ).
  IF sy-subrc IS NOT INITIAL.
* Handle errors
  ENDIF.
  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
* lo_http_client->request->set_method( 'POST' ).
  CALL METHOD lo_http_client->request->set_header_field
    EXPORTING
      name  = '~request_method'
      value = 'POST'.
*  CALL METHOD lo_http_client->request->set_header_field
*    EXPORTING
*      name  = 'content-type:'
*      value = 'application/x-www-form-urlencoded'.

  CALL METHOD lo_http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Type:'
      value = 'application/x-www-form-urlencoded;charset=UTF-8'.


  CALL METHOD lo_http_client->request->set_header_field "We pass header and value
    EXPORTING
      name  = 'X-Naver-Client-Id:'
      value = 'Rh6y2FN_54MoIYIXHdDq'.

    CALL METHOD lo_http_client->request->set_header_field "We pass header and value
    EXPORTING
      name  = 'X-Naver-Client-Secret:'
      value = 'Q4GcM0CQmV'.

*
*  CALL METHOD lo_http_client->request->set_form_field
*    EXPORTING
*      name  = 'grant_type'
*      value = 'password'.
*  CALL METHOD lo_http_client->request->set_form_field
*    EXPORTING
*      name  = 'username'
*      value = 'admin'.
*  CALL METHOD lo_http_client->request->set_form_field
*    EXPORTING
*      name  = 'password'
*      value = 'abc123'.
  CALL METHOD lo_http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4.
  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4.
  IF sy-subrc NE 0.
    DATA subrc TYPE sysubrc.
    DATA errortext TYPE string.
    CALL METHOD lo_http_client->get_last_error
      IMPORTING
        code    = subrc
        message = errortext.
    WRITE: / 'communication_error( receive )',
           / 'code: ', subrc, 'message: ', errortext.
    EXIT.
  ELSE.
****Get the response content in Character format
*  content = client->response->get_cdata( ).
  ENDIF.
**  lo_client->receive( ).
**  IF sy-subrc IS NOT INITIAL.
*** Handle errors
**  ENDIF.
END-OF-SELECTION.
**Return the HTTP body of this entity as binary data
*lv_xstring = lo_http_client->response->get_data( ).
*Return the HTTP body of this entity as string data
  DATA(lv_string2) = lo_http_client->response->get_cdata( ).
*  BREAK-POINT.
  /ui2/cl_json=>deserialize(
    EXPORTING json = lv_string2
              pretty_name = /ui2/cl_json=>pretty_mode-camel_case
    CHANGING  data = t_datos ).

번호 제목 글쓴이 날짜 조회 수
668 REPORT  z05_36 sapjoy 2023.02.07 238
» API sapjoy 2022.11.02 310
666  z15_21_drag sapjoy 2022.02.24 265
665 Z18_06_DRAGCLASS sapjoy 2022.02.22 288
664 REPORT Z21_01 sapjoy 2015.10.28 2242
663 subroutine pool sapjoy 2013.11.15 3269
662 REPORT Z20_01. sapjoy 2012.08.01 5052
661 REPORT Z19_02. sapjoy 2012.08.01 3919
660 REPORT z19_01. sapjoy 2012.08.01 3524
659 REPORT z18_06. sapjoy 2012.08.01 3145
658 REPORT z18_05 sapjoy 2012.08.01 3326
657 REPORT z18_04 . sapjoy 2012.08.01 3340
656 REPORT z18_03. sapjoy 2012.08.01 4549
655 REPORT z18_02. sapjoy 2012.08.01 3070
654 REPORT z18_01. sapjoy 2012.08.01 3107
653 REPORT z17_23 sapjoy 2012.08.01 2508
652 REPORT z17_22 sapjoy 2012.08.01 2259
651 REPORT z17_21 . sapjoy 2012.08.01 2464
650 REPORT z17_20 sapjoy 2012.08.01 2873
649 REPORT z17_19 sapjoy 2012.08.01 2434