a month ago - last edited a month ago
This document explains how to call the HTTP services in ABAP Cloud, from the SAP API Hub
• In this example we obtaining Email address by providing address number and Business Partner Number. Over View •
Step-1:consume an external API from the SAP API Hub
https://api.sap.com/api/API_BUSINESS_PARTNER/resource/Email_ Address
• Step-2: Calling the API in ABAP Cloud Platform.
CLASS zemail DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
METHODS get_email_id IMPORTING addrnr TYPE string bpnumber TYPE string EXPORTING emailid
TYPE string.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zemail IMPLEMENTATION.
METHOD get_email_id.
DATA: lv_url TYPE string,
lo_http_client TYPE REF TO if_web_http_client.
TYPES:
BEGIN OF ty_metadata,
id TYPE string,
uri TYPE string,
type TYPE string,
END OF ty_metadata,
BEGIN OF ty_address_email,
__metadata TYPE ty_metadata,
addressid TYPE string,
person TYPE string,
ordinalnumber TYPE string,
isdefaultemailaddress TYPE string,
emailaddress TYPE string,
searchemailaddress TYPE string,
addresscommunicationremarktext TYPE string,
END OF ty_address_email,
BEGIN OF ty_root,
__count TYPE string,
results TYPE STANDARD TABLE OF ty_address_email WITH EMPTY KEY,
END OF ty_root,
BEGIN OF ty_root1,
d TYPE ty_root,
END OF ty_root1.
DATA : lv_root TYPE ty_root1.
lv_url = |https://sandbox.api.sap.com/s4hanacloud/sap/
/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartnerAddress(BusinessPartner='{ bpnumber
}',AddressID='{ addrnr }')/to_EmailAddress?$top=50&$inlinecount=allpages|.
TRY.
lo_http_client = cl_web_http_client_manager=>create_by_http_destination(
i_destination = cl_http_destination_provider=>create_by_url( i_url = lv_url ) ).
CATCH cx_web_http_client_error cx_http_dest_provider_error.
"handle exception
ENDTRY.
DATA(lo_request) = lo_http_client->get_http_request( ).
lo_request->set_header_fields( VALUE #(
( name = 'Accept' value = 'application/json')
( name = 'DataServiceVersion' value = '2.0' )
)
).
lo_request->set_authorization_basic(
EXPORTING i_username = <User name>
i_password = <password>
RECEIVING r_value = DATA(l_value) ).
TRY. DATA(lv_response) = lo_http_client->execute(
i_method = if_web_http_client=>get )->get_text( ).
CATCH cx_web_http_client_error cx_web_message_error.
"handle exception ENDTRY.
lo_request->get_last_error(
RECEIVING
rc = DATA(lv_error)
).
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_response
CHANGING
data = lv_root ).
LOOP AT lv_root-d-results INTO DATA(wa).
IF emailid = ' '.
emailid = wa-emailaddress.
ELSE.
CONCATENATE emailid wa-emailaddress INTO emailid SEPARATED BY ','.
ENDIF.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
Request clarification before answering.
User | Count |
---|---|
64 | |
10 | |
8 | |
8 | |
7 | |
6 | |
6 | |
6 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.