Calling External REST API From ABAP Program - SAP Q&A
Calling External REST API From ABAP Program - SAP Q&A
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)
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
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.
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.
" 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 ).
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 ).
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( ).
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
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)
).
"-----------------------------------------------"
" 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 = '[email protected]'.
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
<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
Newsletter (https://sap.com/registration/newsletter.html)