
Sample Program
The following ABAP program executes a HTTP request. The request’s destination is the ABAP program itself, as the SAP Web Application Server can function both as a server and as a client.
|
report HTTP_Test. * data declarations data: client type ref to if_http_client. … |
The following parameters must be specified. If nothing is entered, the following default values are used: host (default value: current host), service (default value: port 80 for HTTP and port 443 for HTTPS), protocol (default: HTTP/1.0), path to the required service, as described in
Implementation (default: /sap/public/ping). If the connection using the Destination (SM59) is used when the client object is created, these parameters must be set accordingly in the destination.|
data: host type string value = ‘host.wdf.sap-ag.de’, service type string value = ‘8080’, path type string value = '/sap/public/ping', errortext type string. "used for error handling |
The object of the type
CL_HTTP_CLIENT is created (step 1). As described, there are two possibilities here, which are juxtaposed below.The object
client can be used to access all data (request, response, header field, and so on).The scheme determines whether HTTP or HTTPS is to be used.
|
* create HTTP client object(s) |
|
|
call method cl_http_client=>create exporting host = host_str service = service_str proxy_host = proxy_host proxy_service = proxy_service scheme = scheme importing client = client exceptions argument_not_found = 1 internal_error = 2 plugin_not_active = 3 others = 4. |
call method cl_http_client=>create_by_destination exporting destination = dest importing client = client exceptions destination_not_found = 1 internal_error = 2 argument_not_found = 3 destination_no_authority = 4 plugin_not_active = 5 others = 6. |
|
if sy-subrc <> 0. write: / ‘Create failed, subrc = ', sy-subrc. exit. endif. |
|
Header fields for the requests are set here. Setting the HTTP request method to GET is optional. (If this field is not set, the system uses GET if the HTTP request does not contain a body. Otherwise, it uses POST.) You must set the request URI unless the whole path was specified as a path prefix using the
Destination (SM59). Here you use method set_request_uri of class cl_http_utility.|
* set header fields call method client->request->set_header_field exporting name = '~request_method' value = 'GET'. … * Set request protocol if protocol = 'HTTP/1.0'. call method client->request->set_header_field exporting name = '~server_protocol' value = 'HTTP/1.0'. else. call method client->request->set_header_field exporting name = '~server_protocol' value = 'HTTP/1.1'. endif. cl_http_utility=>set_request_uri( request = client->request uri = uri ). |
Any number of fields in the control block can be filled (step 2).
You can also request that the response is compressed with the gzip-process for sending:
|
* should server send its response in gzip, if possible if cl_http_client=>c_compression_supported = if_http_client=>co_enabled. call method client->request->set_header_field exporting name = 'accept-encoding' value = 'gzip'. endif. endif. |
Additional Options
Now, the request is sent (step 3).
|
* Send call method client->send exporting timeout = timeout exceptions http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 others = 4. if sy-subrc <> 0. call method client->get_last_error importing code = subrc message = errortext. write: / 'communication_error( send )', / 'code: ', subrc, 'message: ', dummy. exit. endif. |
Here, the last error must be queried, using
client->get_last_error.Now, the response is received and the client object is filled with the response data (step 7).
|
* receive call method client->receive exceptions http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 others = 4. if sy-subrc <> 0. call method client->get_last_error importing code = subrc message = errortext. write: / 'communication_error( receive )', / 'code: ', subrc, 'message: ', dummy. exit. endif. |
Exceptions and error queries must be inserted here too.
If the connection is no longer being used, you must close the client object and the connection, as follows:
|
* close call method client->close exceptions http_invalid_state = 1 others = 2. |
To document the successful test, the output routine is called at this point.
|
* output perform write_document. |

The ABAP code above is only a simple example, and is not an example of professional work!