Was this page helpful to you?

Background documentation Example Program: Executing an HTTP Request 

Caution

The ABAP code shown here is simplified sample code. It is not meant as a template for real coding.

The following ABAP program executes an HTTP request. For testing purposes, the call destination is the sending system itself, so that the ABAP Application Server is used in both the client role and the server role:

report HTTP_Test.

* data declarations

 data: client type ref to if_http_client.

 

 

The following parameters must be entered:

Note

If no data is entered, 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).

Note

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

 

Step 1

The object of type CL_HTTP_CLIENT is now created. There are two possible procedures for doing this:

In one case, the create method is called. Here, you need to enter the required host and port, and you can also enter the proxy setting. This overrides any configuration that was made in Transaction SICF (see also Configuring Proxies).

  In the other case, the destination that was maintained in Transaction SM59 is used (method create_by_destination). Only the name of the destination and the client need be specified here. In both cases, you have to define exceptions (not shown here) and query the last error code.

Note

SAP recommends that you use a fixed destination, as this reduces the need for configuration of communication parameters.

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 are then set for the requests. Setting the HTTP request method to GET is optional.

Note

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 http method GET

call method client->request->set_method(

  if_http_request=>co_request_method_get ).

* set protocol version

 if protocol = 'HTTP/1.0'.

  client->request->set_version(

  if_http_request=>co_protocol_version_1_0 ).

 else.

  client->request->set_version(

  if_http_request=>co_protocol_version_1_1 ).

 endif.

* set request uri (/<path>[?<querystring>])

  cl_http_utility=>set_request_uri( request = client->request

 uri  = uri ).

 

Step 2

Any number of fields in the control block can then be filled.

Further Options

  You can deactivate the attributes PROPERTYTYPE_LOGON_POPUP, PROPERTYTYPE_REDIRECT and PROPERTYTYPE_ACCEPT_COMPRESS that have the default value CO_ENABLED (set to CO_DISABLED).

You can set PROPERTYTYPE_ACCEPT_COOKIE to different values. The default value is CO_DISABLED, but values CO_ENABLED (always accept cookies), CO_PROMPT (send a dialog box when cookies are received to ask if the cookie should be accepted) and CO_EVENT (cookie handling using event EVENTKIND_HANDLE_COOKIE of interface IF_HTTP_CLIENT) are also available.

  You can set PROPERTYTYPE_ACCEPT_COMPRESS to the value CO_DISABLED so that the partner can send its data in non-compressed form.

Before the request is sent, you can also enter the necessary logon data using the AUTHENTICATE()method of interface IF_HTTP_CLIENT.

 

Step 3

The request is now sent.

* 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 again, using client -> get_last_error.

Step 4

The response is now received and the client object is filled with the response data.

* 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.

 

Additional Information

Parallelizing Requests

Redirecting Requests