Skip to Content
0

Calling external REST API from ABAP Program

Jul 31, 2017 at 11:08 AM

611

avatar image
Former Member

Hello Experts,

I need to call an external REST API from SAP system (SAP_BASIS 740 SP-lever 9) using abap program (POST method).

I don’t know how to transfer a pdf file to the API. The API’s parameters are: files (list to pdf file), recipients[name] and recipients[email].

DATA: ls_xml TYPE string,
ls_respuesta_xml TYPE string,
ld_urlurl TYPE string,
lo_https_client TYPE REF TO if_http_client,
lo_http_entity TYPE REF TO if_http_entity,
lo_request TYPE REF TO if_rest_entity,
lo_response TYPE REF TO if_rest_entity,
lv_http_status TYPE string,
lv_status TYPE string,
lv_reason TYPE string,
lv_response TYPE string,
lv_content_length TYPE string,
lv_location TYPE string,
lv_content_type TYPE string,
lv_body TYPE string,
lt_tab_file TYPE TABLE OF char255,
ls_xstring TYPE xstring,
lv_length TYPE i,
lv_string TYPE string,
lv_rfc_dest TYPE rfcdest,
lo_http_client TYPE REF TO if_http_client,
lo_rest_client TYPE REF TO cl_rest_http_client,
lo_request_entity TYPE REF TO if_rest_entity,
lt_fields TYPE tihttpnvp,
ls_fields TYPE ihttpnvp.


" Upload PDF file

CALL METHOD cl_gui_frontend_services=>gui_upload

EXPORTING
filename = 'Z:\file.pdf'
filetype = 'BIN'IMPORTING
filelength = lv_lengthCHANGING
data_tab = lt_tab_fileEXCEPTIONS
file_open_error = 01
file_read_error = 02
no_batch = 03
gui_refuse_filetransfer = 04
invalid_type = 05
no_authority = 06
unknown_error = 07
bad_data_format = 08
header_not_allowed = 09
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui = 18.

" PDF conversion to XSTRING

CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'

EXPORTING
input_length = lv_length

IMPORTINGbuffer = ls_xstring

TABLES
binary_tab = lt_tab_file

EXCEPTIONS
failed = 1OTHERS = 2.

" HTTPS
lv_rfc_dest = 'RFC_DEST'. "api.xxxxxxxx.com/v3
cl_http_client=>create_by_destination(

EXPORTING
destination = lv_rfc_dest

IMPORTINGclient = lo_http_client

EXCEPTIONS
argument_not_found = 1
destination_not_found = 2
destination_no_authority = 3
plugin_not_active = 4
internal_error = 5OTHERS = 6 ).

" Logon disable
lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.

" Rest client

CREATE OBJECT lo_rest_client

EXPORTING
io_http_client = lo_http_client.

lo_request = lo_rest_client->if_rest_client~create_request_entity( ).
lo_request->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).


" JSON string
lv_body = '{ "recipients[0][name]":"Test","recipients[0][email]":"test@gmail.com" }'.

lo_request->set_string_data( lv_body ).

ld_urlurl = '/yyyyyyyyy.json'. " or '/yyyyyyyyy.xml is possible'.
ls_fields-name = if_http_header_fields_sap=>request_uri.
ls_fields-value = ld_urlurl.APPEND ls_fields TO lt_fields.

ls_fields-name = 'Authorization'.
ls_fields-value = 'Bearer xxxxxxxxxxxxxxxxxxx'.APPEND ls_fields TO lt_fields.
CALL METHOD lo_rest_client->if_rest_client~set_request_headers

EXPORTING
it_header_fields = lt_fields.


* POST
lo_rest_client->if_rest_resource~post( lo_request ).


* Response
lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
lv_http_status = lv_status = lo_response->get_header_field( '~status_code' ).
lv_reason = lo_response->get_header_field( '~status_reason' ).
lv_content_length = lo_response->get_header_field( 'content-length' ).
lv_location = lo_response->get_header_field( 'location' ).
lv_content_type = lo_response->get_header_field( 'content-type' ).
lv_response = lo_response->get_string_data( ).

lo_rest_client->if_rest_client~close( ).

write: / lv_http_status, lv_reason, lv_response.

Thanks and Best Regards,

Luca

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

avatar image
Former Member Aug 01, 2017 at 10:49 AM
3

Hi Luca,

Try this.

DATA: lo_https_client TYPE REF TO if_http_client,
      lo_http_entity  TYPE REF TO if_http_entity,
      lo_request      TYPE REF TO if_rest_entity,
      lv_http_status  TYPE string,
      lv_status       TYPE string,
      lv_reason       TYPE string,
      lv_offset       TYPE i,
      lv_response     TYPE string,
      lt_tab_file     TYPE TABLE OF char255,
      ls_xstring      TYPE xstring,
      lv_length       TYPE i,
      lo_http_client  TYPE REF TO if_http_client,
      lo_rest_client  TYPE REF TO cl_rest_http_client,
      lo_response     TYPE REF TO if_rest_entity,
      lo_post_file    TYPE REF TO cl_rest_multipart_form_data,
      lt_headers      TYPE tihttpnvp.
FIELD-SYMBOLS: <fs_headers> TYPE LINE OF tihttpnvp.
*--------------------------------------------------------------------*
CALL METHOD cl_http_client=>create_by_url
  EXPORTING
   url                = 'http://httpbin.org/post'
  IMPORTING
    client             = lo_http_client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4.
IF sy-subrc IS INITIAL.
  "-----------------------------------------------"
  " Carga de fichero
  "-----------------------------------------------"
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                = 'C:\Users\XXXX\Desktop\Test.pdf'
      filetype                = 'BIN'
    IMPORTING
      filelength              = lv_length
    CHANGING
      data_tab                = lt_tab_file
    EXCEPTIONS
      file_open_error         = 01
      file_read_error         = 02
      no_batch                = 03
      gui_refuse_filetransfer = 04
      invalid_type            = 05
      no_authority            = 06
      unknown_error           = 07
      bad_data_format         = 08
      header_not_allowed      = 09
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      not_supported_by_gui    = 17
      error_no_gui            = 18.
  " PDF conversion to XSTRING
  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    EXPORTING
      input_length = lv_length
    IMPORTING
      buffer       = ls_xstring
    TABLES
      binary_tab   = lt_tab_file
    EXCEPTIONS
      failed       = 1
      OTHERS       = 2.
  "-----------------------------------------------"
  " Cliente
  "-----------------------------------------------"
  CREATE OBJECT lo_rest_client
    EXPORTING
      io_http_client = lo_http_client.
  lo_request = lo_rest_client->if_rest_client~create_request_entity( ).
  lo_request->set_content_type(
     iv_media_type = if_rest_media_type=>gc_multipart_form_data
  ).
  "-----------------------------------------------"
  " Post fields
  "-----------------------------------------------"
  CREATE OBJECT lo_post_file
    EXPORTING
      io_entity = lo_request.
  CALL METHOD lo_post_file->set_form_field
    EXPORTING
      iv_name  = 'recipients[0][name]'
      iv_value = 'Test'.
  CALL METHOD lo_post_file->set_form_field
    EXPORTING
      iv_name  = 'recipients[0][email]'
      iv_value = 'test@gmail.com'.
  CALL METHOD lo_post_file->set_file
    EXPORTING
      iv_name     = 'PDF_File'
      iv_filename = 'file.pdf'
      iv_type     = 'PDF'
      iv_data     = ls_xstring.
  "-----------------------------------------------"
  " Post a Request
  "-----------------------------------------------"
  CALL METHOD lo_post_file->if_rest_entity_provider~write_to
    EXPORTING
      io_entity = lo_request.
  "-----------------------------------------------"
  " Headers
  "-----------------------------------------------"
  CALL METHOD lo_rest_client->if_rest_client~set_request_headers
    EXPORTING
      it_header_fields = lt_headers.
  "-----------------------------------------------"
  " Envio
  "-----------------------------------------------"
  lo_rest_client->if_rest_resource~post( lo_request ).
  "-----------------------------------------------"
  " Respuestas
  "-----------------------------------------------"
  lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
  lv_http_status = lv_status = lo_response->get_header_field( '~status_code' ).
  lv_reason = lo_response->get_header_field( '~status_reason' ).
  lv_response = lo_response->get_string_data( ).
  lo_rest_client->if_rest_client~close( ).
  "-----------------------------------------------"
  " Salida
  "-----------------------------------------------"
  WRITE: / lv_http_status,
         / lv_reason.
  DO 1000 TIMES.
    TRY .
        WRITE / lv_response+lv_offset(100).
      CATCH cx_root.
        EXIT.
    ENDTRY.
    ADD 100 TO lv_offset.
  ENDDO.
ENDIF.
Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hello German,

your solution is correct! Thanks so much!!

Best Regards,

Luca

0

Thanks This solved my problem too. I was trying to post an image to rest API using following approach but it was not working. API was not able to recognize image file

****************does mot Work********************************************
DATA(lo_mulipart) = lo_http_client->request->add_multipart( ).

  lo_http_client->request->set_header_field( name = 'Accept'
                                 value = 'application/json' ).
  lo_mulipart->set_header_field( name = 'Content-Disposition'
                                value = lv_file_form_data ).

  lo_mulipart->set_header_field( name = 'Content-Type'
                                value = 'image/png' ).
  lo_mulipart->set_header_field( name = 'Content-Transfer-Encoding'
                                value = 'binary' ).
  lo_mulipart->set_data( data = lv_file_content
                         offset = 0
                         length = lv_input_len ).

<br>
0