Show TOC

Background documentationExample program: Executing an HTTP Request Locate this document in the navigation structure

 

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

Caution Caution

This program shows a simplified sample code. It is not meant as a template for real coding.

End of the caution.

Syntax Syntax

  1. report HTTP_Test.
  2. * data declarations
  3.   data: client type ref to if_http_client.
End of the code.

The following parameters must be entered:

Note Note

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

End of the note.

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

End of the note.

Syntax Syntax

  1. data: host type string value = ‘host.wdf.sap-ag.de’,
  2.       service type string value = ‘8080’,
  3.       path type string value = '/sap/public/ping',
  4.       errortext type string. "used for error handling
End of the code.
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 Note

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

    End of the note.

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)

Syntax Syntax

  1. call method cl_http_client=>create
  2.  exporting host  = host_str
  3.    service       = service_str
  4.    proxy_host    = proxy_host
  5.    proxy_service = proxy_service
  6.    scheme        = scheme
  7.  importing client  = client
  8. exceptions
  9.          argument_not_found = 1
  10.          internal_error     = 2
  11.          plugin_not_active  = 3
  12.          others             = 4.
End of the code.

Syntax Syntax

  1. call method cl_http_client=>create_by_destination
  2.   exporting destination = dest
  3.   importing client      = client
  4. exceptions
  5.         destination_not_found    = 1
  6.         internal_error           = 2
  7.         argument_not_found       = 3
  8.         destination_no_authority = 4
  9.         plugin_not_active        = 5
  10.         others                   = 6.
End of the code.

Syntax Syntax

  1. if sy-subrc <>0. 
  2.   write: / ‘Create failed, subrc = ', sy-subrc.
  3.   exit.
  4. endif.
End of the code.

Header fields are then set for the requests. Setting the HTTP request method to GET is optional.

Note Note

If this field is not set, the system uses GET if the HTTP request does not contain a body. Otherwise, it uses POST.

End of the note.

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.

Syntax Syntax

  1. * set http method GET
  2. call method client-( 
  3.                    if_http_request= ).
  4. * set protocol version
  5.    if protocol = 'HTTP/1.0'.
  6.        client-(
  7.             if_http_request= ).
  8.    else.
  9.       client-(
  10.             if_http_request= ).
  11.    endif.
  12. * set request uri (/>[?>])
  13.    cl_http_utility=( request = client-
  14.                                       uri    = uri ).
End of the code.
Step 2

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

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

Syntax Syntax

  1. * Send
  2. call method client-send
  3.   exporting  timeout = timeout
  4.   exceptions http_communication_failure  = 1
  5.              http_invalid_state          = 2
  6.              http_processing_failed      = 3
  7.              others                      = 4.
  8. if sy-subrc < 0.
  9.   call method client-get_last_error
  10.      importing code    = subrc
  11.                message = errortext.
  12.   write: / 'communication_error( send )',
  13.          / 'code: ', subrc, 'message: ', dummy.
  14.   exit.
  15. endif.
End of the code.

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.

Syntax Syntax

  1. * receive
  2. call method client->receive
  3.     exceptions http_communication_failure  = 1
  4.                http_invalid_state          = 2
  5.                http_processing_failed      = 3
  6.                others                      = 4.
  7.   if sy-subrc <> 0.
  8.      call method client->get_last_error
  9.      importing code    = subrc
  10.                message = errortext.
  11.     write: / 'communication_error( receive )',
  12.            / 'code: ', subrc, 'message: ', dummy.
  13.     exit.
  14.   endif.
End of the code.

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:

Syntax Syntax

  1. * close
  2. call method client->close
  3.   exceptions http_invalid_state   = 1               
  4.              others               = 2.
End of the code.

To document the successful test, the output routine is called at this point.

Syntax Syntax

  1. * output
  2.   perform write_document.
End of the code.