메뉴 건너뛰기

SAP 한국 커뮤니티

API

sapjoy 2022.11.02 19:30 조회 수 : 319

*&---------------------------------------------------------------------*
*& 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 ).

번호 제목 글쓴이 날짜 조회 수
670 Z01_012 [9] sapjoy 2006.12.02 4546
669 REPORT Z03_005 [1] sapjoy 2006.12.02 2735
668 REPORT Z03_006 [5] sapjoy 2006.12.02 2812
667 Z01_018 [2] sapjoy 2006.12.02 3042
666 Z01_019 [2] sapjoy 2006.12.02 2688
665 Z01_020 [2] sapjoy 2006.12.02 3008
664 Z01_021 [1] sapjoy 2006.12.02 2771
663 REPORT Z03_018 [3] sapjoy 2006.12.02 2605
662 Z02_003 [2] sapjoy 2006.12.02 3042
661 REPORT Z02_005 . sapjoy 2006.12.02 2375
660 REPORT Z04_005 . sapjoy 2006.12.02 2595
659 <b>ABAP SOURCE는 교재에 사용된 소스들입니다.</b> [1] sapjoy 2006.12.02 3618
658 Dynamic Table Maintenance [1] sapjoy 2006.12.04 4094
657 REPORT Z06_001 MESSAGE-ID ZTEST . [1] sapjoy 2006.12.05 3205
656 REPORT Z07_002 . sapjoy 2006.12.06 2755
655 Z07_001 sapjoy 2006.12.06 2396
654 Z07_005 sapjoy 2006.12.06 2353
653 REPORT Z07_007 sapjoy 2006.12.07 2698
652 REPORT Z07_008 sapjoy 2006.12.07 2462
651 REPORT Z07_009 sapjoy 2006.12.07 2258