CL_HTTP_CLIENT¶
Make HTTP/HTTPS requests from ABAP to external REST APIs or web services.
Purpose¶
CL_HTTP_CLIENT is ABAP's built-in HTTP client. It supports GET, POST, PUT, DELETE, custom headers, basic auth, and SSL/TLS. Use it to call REST APIs, webhooks, or any HTTP endpoint reachable from the SAP application server.
SM59 destinations (recommended for production)
CREATE_BY_DESTINATION reads host, port, SSL settings, and credentials from an SM59
HTTP destination. This keeps configuration out of code and is the preferred approach
for production systems. CREATE_BY_URL is convenient for prototyping.
Key methods¶
| Method | Called on | Description |
|---|---|---|
CREATE_BY_URL |
CL_HTTP_CLIENT (static) |
Create client from a literal URL |
CREATE_BY_DESTINATION |
CL_HTTP_CLIENT (static) |
Create client from SM59 destination name |
set_method |
client->request |
Set HTTP verb: 'GET', 'POST', 'PUT', 'DELETE' |
set_header_field |
client->request |
Set a request header (name / value) |
set_cdata |
client->request |
Set text request body (string — UTF-8) |
set_data |
client->request |
Set binary request body (xstring) |
send |
client |
Send the request |
receive |
client |
Receive the response (blocks until complete) |
get_status |
client->response |
Read HTTP status code into integer |
get_header_field |
client->response |
Read a response header value |
get_cdata |
client->response |
Read text response body (string) |
get_data |
client->response |
Read binary response body (xstring) |
close |
client |
Close TCP connection — always call in FINALLY |
Examples¶
GET request¶
DATA lo_client TYPE REF TO if_http_client.
DATA lv_url TYPE string VALUE 'https://api.example.com/items/42'.
DATA lv_body TYPE string.
DATA lv_status TYPE i.
DATA lv_reason TYPE string.
cl_http_client=>create_by_url(
EXPORTING
url = lv_url
IMPORTING
client = lo_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE cx_static_check. " or MESSAGE
ENDIF.
TRY.
lo_client->request->set_method( if_http_request=>co_request_method_get ).
lo_client->request->set_header_field(
name = 'Accept'
value = 'application/json' ).
lo_client->send(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
OTHERS = 3 ).
lo_client->receive(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4 ).
lo_client->response->get_status(
IMPORTING
code = lv_status
reason = lv_reason ).
lv_body = lo_client->response->get_cdata( ).
IF lv_status = 200.
" process lv_body (JSON string)
ELSE.
MESSAGE |HTTP { lv_status }: { lv_reason }| TYPE 'E'.
ENDIF.
FINALLY.
lo_client->close( ).
ENDTRY.
POST with JSON body¶
DATA lv_json TYPE string.
lv_json = '{"name":"Test Item","qty":5}'.
lo_client->request->set_method( if_http_request=>co_request_method_post ).
lo_client->request->set_header_field(
name = 'Content-Type'
value = 'application/json' ).
lo_client->request->set_header_field(
name = 'Accept'
value = 'application/json' ).
lo_client->request->set_cdata( lv_json ).
lo_client->send(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
OTHERS = 3 ).
lo_client->receive(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4 ).
lo_client->response->get_status(
IMPORTING
code = lv_status
reason = lv_reason ).
lv_body = lo_client->response->get_cdata( ).
Using an SM59 destination¶
cl_http_client=>create_by_destination(
EXPORTING
destination = 'MY_REST_API' " defined in SM59
IMPORTING
client = lo_client
EXCEPTIONS
argument_not_found = 1
destination_not_found = 2
internal_error = 3
OTHERS = 4 ).
Common pitfalls¶
- SSL / HTTPS: the server certificate must be imported into the SAP trust store via STRUST (SSL client identity
ANONYMor a dedicated one). Without it,send()raisesHTTP_COMMUNICATION_FAILURE. - Always close: call
lo_client->close()in aFINALLYblock. Unclosed connections exhaust the HTTP connection pool. - Text vs. binary: use
get_cdata()/set_cdata()for JSON and XML (character data). Useget_data()/set_data()for binary payloads (images, PDFs, etc.). - Basic auth via URL: embedding credentials in the URL (
http://user:pass@host) works but is insecure. Store credentials in SM59 or useset_header_fieldwith a Base64-encodedAuthorizationheader. - Proxy: if the SAP system accesses the internet via a proxy, configure it in SICF / SM59 HTTP settings or use
lo_client->propertytype_proxy_host.
Response body encoding
get_cdata() returns the body re-encoded to ABAP's internal UTF-16. If the API
returns a charset other than UTF-8 and the conversion fails, use get_data() and
handle the raw bytes manually.
See also¶
- Tutorial: calling a REST API from ABAP
- Transaction SM59 — HTTP/HTTPS destination configuration
- Transaction STRUST — SSL certificate management
IF_HTTP_CLIENT,IF_HTTP_REQUEST,IF_HTTP_RESPONSE— underlying interfaces