Difference between revisions of "SAP ABAP HTTP"

From SapWiki
(Created page with " *----------------------------------------------------------------------* * FORM http_client *----------------------------------------------------------------------*...")
 
Line 1: Line 1:
 +
==Ejemplo HTTP_CLIENT==
 +
 +
URL              https://qa.sapwiki.cl:443/polosur/DLEApi.php
 +
NAMESPACE        https://qa.sapwiki.cl/polosur/
 +
HOST            qa.sapwiki.cl
 +
SOAP ACTION      https://qa.sapwiki.cl/polosur/CargarDocumento
 +
 
  *----------------------------------------------------------------------*
 
  *----------------------------------------------------------------------*
 
  *      FORM http_client
 
  *      FORM http_client

Revision as of 21:11, 26 March 2020

Ejemplo HTTP_CLIENT

URL              https://qa.sapwiki.cl:443/polosur/DLEApi.php
NAMESPACE        https://qa.sapwiki.cl/polosur/
HOST             qa.sapwiki.cl
SOAP ACTION      https://qa.sapwiki.cl/polosur/CargarDocumento
*----------------------------------------------------------------------*
*       FORM http_client
*----------------------------------------------------------------------*
*       basado en programa RSWF_TEST_HTTP
*----------------------------------------------------------------------*
*  --> p_url          Ej.: https://qa.sapwiki.cl:443/polosur/DLEApi.php
*  --> p_soap         String <soapenv:Envelope...</soapenv:Envelope>
*  --> p_soap_action  Parámetro SOAPAction:
*  --> p_host         Parámetro Host:

*  <--  p2        text
*----------------------------------------------------------------------*

FORM http_client USING p_url
                       p_soap
                       p_soap_action
                       p_host
              CHANGING p_subrc
                       p_msg
                       p_response.

  TYPE-POOLS: swfxc, icon.
  DATA: l_http_client        TYPE REF TO if_http_client.
  DATA: l_url                TYPE string.
  DATA: l_code               TYPE sy-subrc.
  DATA: l_code_string        TYPE string.
  DATA: l_message_string     TYPE string.
  DATA: lt_http_fields       TYPE tihttpnvp.
  DATA: l_http_field_wa      TYPE ihttpnvp.
  DATA: l_char_header(40)    TYPE c.
  DATA: l_body_string        TYPE string.
*-----------------------------------------------------------------------
*- Create the HTTP-Client
*-----------------------------------------------------------------------

  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = p_url
*     PROXY_HOST         = l_proxy_host
*     PROXY_SERVICE      = l_proxy_service
      ssl_id             = 'ANONYM'
    IMPORTING
      client             = l_http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
  IF sy-subrc <> 0.
*    MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
*            into l_message_string.
    IF l_message_string IS INITIAL.
      l_message_string = 'Error creating HTTP-Client'.      "#EC NOTEXT
    ENDIF.
*    WRITE: / icon_red_light AS ICON.
*    PERFORM print_string USING l_message_string.
    p_msg = l_message_string.
    p_subrc = 9.

    EXIT.
  ENDIF.

*--------------------------------------------------------------------*
* HEADER
*--------------------------------------------------------------------*

*  CALL METHOD l_http_client->request->set_header_field
*    EXPORTING
*      name  = '~request_method'
*      value = 'POST'.

  DATA(l_bytes) = strlen( p_soap ).
  DATA l_content_length TYPE string.

  MOVE l_bytes TO l_content_length.
  CONDENSE l_content_length.

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'Accept-Encoding'
      value = 'gzip,deflate'.   "#EC NOTEXT

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Type'
      value = 'text/xml;charset=UTF-8'. "#EC NOTEXT

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'SOAPAction'
      value = p_soap_action. "#EC NOTEXT "'"https://qa.sapwiki.cl/polosur/CargarDocumento"'.

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'Host'
      value = p_host. "#EC NOTEXT "'qa.cloudsigner.cl'.

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'Connection'
      value = 'Keep-Alive'.    "#EC NOTEXT

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'User-Agent'
      value = 'SAP NetWeaver Application Server (1.0;740)'. "#EC NOTEXT

  CALL METHOD l_http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Length'
      value = l_content_length. "#EC NOTEXT "'30000'.

*--------------------------------------------------------------------*
* soapenvelope
* string <soapenv:Envelope...</soapenv:Envelope>
*--------------------------------------------------------------------*
  CALL METHOD l_http_client->request->set_cdata
    EXPORTING
      data = p_soap.
*    offset = 0
*    length = '19000'.

** copiado de CL_SOAP_HTTP_TPBND_ROOT=======CM01V
** provide the compression settings to the HTTP client object
**  a) compress request
*  IF 1 = 1. "m_compress_request EQ tsrtp_f_bdg_compress_true.
*    CALL METHOD l_http_client->set_compression
*      EXPORTING
*        options                  = if_http_client=>co_compress_in_all_cases
*      EXCEPTIONS
*        compression_not_possible = 1.                       "#EC *
** ignore this exception to have the same behaviour as for clients created
** via sm59-destination: for this clients the method set_compression is
** called within the method SEND without exception handling!
*  ENDIF.
*
*
** b) compress response
*  IF 1 = 1 ."m_compress_response EQ tsrtp_f_bdg_compress_true.
*    l_http_client->propertytype_accept_compress = if_http_client=>co_enabled.
*  ELSE.
**    m_client->propertytype_accept_compress = if_http_client=>co_disabled.
*  ENDIF.
*
** according to RFC2612, section 10.3. (10.3.2, 10.3.3, 10.3.8)
** the HTTP client is not allowed to follow an redirect automatically
** for methods other than HEAD and GET => prevent this for WS calls
** NOTE: prevent the redirect also for PING functionalty although HEAD is
** used in this case in order to get an reliable PING-result
*  l_http_client->propertytype_redirect = if_http_client=>co_disabled.
*-----------------------------------------------------------------------
*- send the http post
*-----------------------------------------------------------------------
  CALL METHOD l_http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
  IF sy-subrc <> 0.
*    WRITE: / icon_red_light AS ICON.

    CALL METHOD l_http_client->get_last_error
      IMPORTING
        code    = l_code
        message = l_message_string.

    CALL METHOD l_http_client->close.

    l_code_string = l_code.
    CONCATENATE 'HTTP-Send: RC=' l_code_string              "#EC NOTEXT
           INTO l_code_string .
    CONCATENATE l_code_string l_message_string
           INTO l_message_string SEPARATED BY space.

*    PERFORM print_string USING l_message_string.
    p_msg = l_message_string.
    p_subrc = 8.
    EXIT.
  ENDIF.

*-----------------------------------------------------------------------
*- receive the result of http post
*-----------------------------------------------------------------------
  CALL METHOD l_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.
  IF sy-subrc <> 0.
*    WRITE: / icon_red_light AS ICON.

    CALL METHOD l_http_client->get_last_error
      IMPORTING
        code    = l_code
        message = l_message_string.

    CALL METHOD l_http_client->close.

    l_code_string = l_code.
    CONCATENATE 'HTTP-Receive: RC=' l_code_string           "#EC NOTEXT
           INTO l_code_string .
    CONCATENATE l_code_string l_message_string
           INTO l_message_string SEPARATED BY space.

*    PERFORM print_string USING l_message_string.
    p_msg = l_message_string.
    p_subrc = 7.
    EXIT.
  ENDIF.

*-----------------------------------------------------------------------
*- print the results
*-----------------------------------------------------------------------
  CALL METHOD l_http_client->response->get_status
    IMPORTING
      code = l_code.
  IF l_code < 300.
*   HTTP-codes: 100 - 199 = informations
*   HTTP-codes: 200 - 299 = client-request successful (200 = OK)
*    WRITE: / icon_green_light AS ICON.
    p_subrc = 0.
  ELSE.
*   HTTP-codes: 300 - 399 = redirected; further actions required
*   HTTP-codes: 400 - 499 = client-request incomplete
*   HTTP-codes: 500 - 599 = server errors
*    WRITE: / icon_red_light AS ICON.
*    IF l_code BETWEEN 300 AND 399.
*      p_msg = 'Redirected; further actions required'.       "#EC NOTEXT
*    ELSEIF  l_code BETWEEN 400 AND 499.
*      p_msg = 'Client-request incomplete'.                  "#EC NOTEXT
*    ELSE.
*      p_msg = 'Server errors'.                              "#EC NOTEXT
*    ENDIF.
*
*    p_msg = |Cloudsigner: Error { l_code } { p_msg }|.      "#EC NOTEXT

    p_subrc = 6.
  ENDIF.
*-- get the http header fields
*  CALL METHOD l_http_client->response->get_header_fields
*    CHANGING
*      fields = lt_http_fields.
*  LOOP AT lt_http_fields INTO l_http_field_wa.
*    l_char_header = l_http_field_wa-name.
*    WRITE: / l_char_header.
*    l_char_header = l_http_field_wa-value.
*    WRITE: l_char_header.
*  ENDLOOP.
*  WRITE: / sy-uline.

*- get the body
  IF p_subrc = 0.
    l_body_string = l_http_client->response->get_cdata(  ).
*  PERFORM print_string USING l_body_string.
    p_response = l_body_string.
  ELSE.
*-- get the http header fields
    CALL METHOD l_http_client->response->get_header_fields
      CHANGING
        fields = lt_http_fields.
    LOOP AT lt_http_fields INTO l_http_field_wa.
      p_msg = |Webservice: Error { l_http_field_wa-value }|.
      EXIT.
    ENDLOOP.
  ENDIF.

ENDFORM.