0% found this document useful (0 votes)
2K views

Calling External REST API From ABAP Program - SAP Q&A

Calling External REST API From ABAP Program - SAP Q&A

Uploaded by

SS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Calling External REST API From ABAP Program - SAP Q&A

Calling External REST API From ABAP Program - SAP Q&A

Uploaded by

SS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

(http://www.sap.

com/)

ts Products (https://www.sap.com/products.html)
Industries Industries (https://www.sap.com/industries.html)

es and Support
Services and Support Training
(https://www.sap.com/support.html) Training (https://www.sap.com/training-certi cation.html)

nity Community (https://www.sap.com/community.html)


Developer Developer (https://developers.sap.com/index.html)
Partner Partner (https://www.sap.com/partner.html)

About (https://www.sap.com/corporate/en.html)

 
Ask a Question (https://answers.sap.com/questions/ask.html) Write a Blog Post (https://blogs.sap.com/wp-admin/post-new.php) Login (/users

Not what you're looking for? Search community questions.

Former Member
Calling external REST API from ABAP Program
Jul 31, 2017 at 11:08 AM | 5.3k Views
ions%2F271783%2Fcalling-external-rest-api-from-abap-
_data%3D271783%26s_csrf%3D1562163438895.8157)
0
ions%2F271783%2Fcalling-external-rest-api-from-abap-
s_data%3D271783%26s_csrf%3D1562163438895.8157)
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 (http://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 (http://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 (mailto: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
ABAP Development (/tags/833755570260738661924709785639136) | NW ABAP Web Services (/tags/99891761267046184358097136821575)
Follow (/users/login.html?redirect_to=%2Fquestions%2F271783%2Fcalling-external-rest-api-from-abap-program.html%3Fs_action%3Dfollow%26s_csrf%3D1562163438895.8157)

RSS Feed

Related questions
Calling REST API Get Method from SAP ABAP (https://answers.sap.com/questions/735732/calling-rest-api-get-method-from-sap-abap.html)
By Lakshmanan Jagannathan Kannan ( https://people.sap.com/lakshmanjk) , Jan 18, 2019
How to convert JSON response from REST API to ABAP Structures? (https://answers.sap.com/questions/383255/how-to-convert-json-response-from-rest-api-to-abap.html)
By Former Member ( https://people.sap.com/former.member) , Dec 12, 2017

1 Answer
Votes | Newest | Oldest

Former Member

Aug 01, 2017 at 10:49 AM

Hi Luca,
tml?redirect_to=%2Fquestions%2F271783%2Fcalling-external-rest-api-
%3DvoteUp%26s_data%3D272102%26s_csrf%3D1562163438895.8157)
bap-program.html%3FchildToView%3D272102%23answer-
3 Try this.
tml?redirect_to=%2Fquestions%2F271783%2Fcalling-external-rest-api-
bap-program.html%3FchildToView%3D272102%23answer-
3DvoteDown%26s_data%3D272102%26s_csrf%3D1562163438895.8157)

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
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.

Share

2 Comments

Ajay Rawat (/users/133814/ajayrawat.html)

Mar 18, 2018 at 06:07 AM (/comments/460119/view.html)


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 le

****************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>

Like (/users/login.html?redirect_to=%2Fquestions%2F271783%2Fcalling-external-rest-api-from-abap-program.html%3FchildToView%3D460119%23comment-
460119%26s_action%3Dlike_comment%26s_data%3D460119%26s_csrf%3D1562163438895.8157)
0
Share

Show all

Share & Follow

(https://www.facebook.com/sapcommunity) (https://twitter.com/SAPCommunity) (https://www.youtube.com/c/SAPCommunities)

(https://www.linkedin.com/company/sap) (http://www.slideshare.net/SAP) (https://instagram.com/sap/) ()

Privacy (http://sap.com/about/legal/privacy.html) Terms of Use (http://sap.com/corporate/en/legal/terms-of-use.html)

Legal Disclosure (http://sap.com/about/legal/impressum.html) Copyright (http://sap.com/about/legal/copyright.html)

Trademark (http://sap.com/about/legal/trademark.html) Sitemap (http://www.sap.com/sitemap/index.html)

Newsletter (https://sap.com/registration/newsletter.html)

You might also like