Consume SAP CPI HTTP API in ABAP Using SM59

 

Consume SAP CPI HTTP API in ABAP Using SM59 (With /http Prefix & Client ID/Secret)




This guide shows a clean and reusable approach to call SAP CPI APIs from ABAP with:

  • SM59 RFC Destination

  • Prefix /http maintained in SM59

  • Dynamic endpoint handling

  • Client ID & Secret (Basic Authentication)

  • Custom configuration table (no hardcoding)


📌 Scenario Design

🔹 Key Concept

  • SM59: Maintain host + /http prefix

  • Table: Maintain only remaining endpoint

  • ABAP: Combines both dynamically


Example:

ComponentValue
SM59 Hostdev-cpi-xxxx.cfapps.ap11.hana.ondemand.com
SM59 Prefix/http
Table Endpoint/transaction
Final URL/http/transaction

🔧 Step 1: Create RFC Destination (SM59)

  1. Go to SM59

  2. Create destination:

    • Type: G (HTTP Connection)

Maintain:

Technical Settings:

  • Target Host: dev-cpi-xxxx.cfapps.ap11.hana.ondemand.com

  • Service No: 443

  • Path Prefix: /http(Important)

👉 Now /http is fixed and reusable.


🗂️ Step 2: Create Custom Config Table

Create a table: ZCPI_CONFIG

Suggested Structure:

FIELD NAMEDESCRIPTION
PROCESSKey (DEST / API / AUTH)
VALUEStores destination or endpoint
CLIENT_IDClient ID
CLIENT_SECRETClient Secret

Sample Entries:

PROCESSVALUECLIENT_IDCLIENT_SECRET
DESTZCPI_DEST--
API/transaction--
AUTH-CPI_CLIENTCPI_SECRET

💻 Step 3: ABAP Code (With Comments)

DATA: lo_http_client TYPE REF TO if_http_client,
      lv_json        TYPE string,
      lv_response    TYPE string,
      lv_endpoint    TYPE string,
      lv_client      TYPE string,
      lv_secret      TYPE string.

DATA: ls_dest TYPE zcpi_config,
      ls_api  TYPE zcpi_config,
      ls_auth TYPE zcpi_config.

" 1. Read RFC Destination
SELECT SINGLE * INTO ls_dest FROM zcpi_config WHERE process = 'DEST'.

" 2. Read API Endpoint (only /transaction)
SELECT SINGLE * INTO ls_api FROM zcpi_config WHERE process = 'API'.
lv_endpoint = ls_api-value.

IF ls_dest-value IS INITIAL.
  WRITE: 'Maintain RFC destination in ZCPI_CONFIG'.
  EXIT.
ENDIF.

IF lv_endpoint IS INITIAL.
  WRITE: 'Maintain API endpoint'.
  EXIT.
ENDIF.

" 3. Create HTTP Client using SM59
CALL METHOD cl_http_client=>create_by_destination
  EXPORTING
    destination = ls_dest-value
  IMPORTING
    client      = lo_http_client.

IF sy-subrc <> 0.
  WRITE: 'Error creating HTTP client'.
  EXIT.
ENDIF.

" 4. Append Endpoint to Prefix (/http already in SM59)
lo_http_client->request->set_header_field(
  name  = '~request_uri'
  value = lv_endpoint ).   " Only /transaction

" Final URL becomes: /http/transaction

" 5. Read Authentication Details
SELECT SINGLE * INTO ls_auth FROM zcpi_config WHERE process = 'AUTH'.

IF ls_auth-client_id IS INITIAL OR ls_auth-client_secret IS INITIAL.
  WRITE: 'Maintain Client ID & Secret'.
  EXIT.
ENDIF.

lv_client = ls_auth-client_id.
lv_secret = ls_auth-client_secret.

" 6. Set Basic Authentication
lo_http_client->authenticate(
  username = lv_client
  password = lv_secret ).

" 7. Set HTTP Method & Headers
lo_http_client->request->set_method( if_http_request=>co_request_method_post ).

lo_http_client->request->set_header_field(
  name  = 'Content-Type'
  value = 'application/json' ).

lo_http_client->request->set_header_field(
  name  = 'Accept'
  value = '*/*' ).

" 8. Prepare JSON Payload
lv_json = '{ "id": "1001", "type": "TEST" }'.

lo_http_client->request->set_cdata( lv_json ).

" 9. Send Request
CALL METHOD lo_http_client->send
  EXPORTING timeout = 15.

" 10. Receive Response
CALL METHOD lo_http_client->receive.

" 11. Fetch Response
lv_response = lo_http_client->response->get_cdata( ).

DATA(status_code) TYPE i.
DATA(reason) TYPE string.

lo_http_client->response->get_status(
  IMPORTING
    code   = status_code
    reason = reason ).

WRITE: / 'Status:', status_code.
WRITE: / 'Response:', lv_response.

" 12. Close Connection
lo_http_client->close( ).

🎯 Key Highlights

✅ Prefix Handling (Best Practice)

  • /http → maintained in SM59

  • /transaction → maintained in table

  • Combined dynamically → flexible & reusable


✅ Reusable Architecture

  • One RFC → multiple APIs

  • No hardcoding

  • Easy maintenance


✅ Secure Authentication

  • Client ID & Secret stored in table

  • Passed using:

lo_http_client->authenticate( )

⚠️ Common Issues

IssueReasonFix
404 ErrorWrong endpointCheck /transaction
401 ErrorInvalid credentialsVerify client ID/secret
SSL IssueCertificate missingMaintain in STRUST
No responseTimeoutCheck network

🏁 Final Summary

This design gives you:

  • 🔁 Reusable RFC destination

  • Dynamic endpoint control

  • 🔐 Simple authentication handling

  • 🧩 Clean separation (SM59 + Table + Code)


If you want, I can next help you build:

  • Reusable ABAP class (factory pattern)

  • CPI integration logging dashboard

  • Error handling + retry framework

Just let me know 👍

Post a Comment

0 Comments

Total Pageviews