Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Send xml file from sap to third party url through https

Former Member
0 Likes
8,693

Hi,

I have a requirement to send the xml file from ecc to a 3rd party url through HTTPS. How can we achieve this using ABAP.

Client doesn't have XI enviroment. The client has provided the 3rd party url where the file needs to be uploaded.

Please help ! <removed by moderator>

Thanks in advance.

Regards,

Chitra.K

Edited by: Thomas Zloch on Sep 12, 2011 12:58 PM

4 REPLIES 4
Read only

Amey-Mogare
Contributor
0 Likes
3,446

Hi Chitra,

I had similar requirement and here is what I did: -


REPORT  Z_HTTP_POST_TEST_AMEY.

DATA: L_URL               TYPE                   STRING          ,
      L_PARAMS_STRING     TYPE                   STRING          ,
      L_HTTP_CLIENT       TYPE REF TO            IF_HTTP_CLIENT  ,
      L_RESULT            TYPE                   STRING          ,
      L_STATUS_TEXT       TYPE                   STRING          ,
      L_HTTP_STATUS_CODE  TYPE                   I               ,
      L_HTTP_LENGTH       TYPE                   I               ,
      L_PARAMS_XSTRING    TYPE                   XSTRING         ,
      L_XSTRING           TYPE                   XSTRING         ,
      L_IS_XML_TABLE      TYPE STANDARD TABLE OF SMUM_XMLTB      ,
      L_IS_RETURN         TYPE STANDARD TABLE OF BAPIRET2        ,
      L_OUT_TAB           TYPE STANDARD TABLE OF TBL1024
      .

MOVE 'https://<hostname>/xxx/yyy/zzz' TO L_URL.
MOVE '<XML as string>' TO L_PARAMS_STRING.

*STEP-1 : CREATE HTTP CLIENT
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
    EXPORTING
      URL                = L_URL
    IMPORTING
      CLIENT             = L_HTTP_CLIENT
    EXCEPTIONS
      ARGUMENT_NOT_FOUND = 1
      PLUGIN_NOT_ACTIVE  = 2
      INTERNAL_ERROR     = 3
      OTHERS             = 4 .

"STEP-2 :  AUTHENTICATE HTTP CLIENT
CALL METHOD L_HTTP_CLIENT->AUTHENTICATE
  EXPORTING
    USERNAME             = 'testUser'
    PASSWORD             = 'testPassword'.

 "STEP-3 :  SET HTTP HEADERS
 CALL METHOD L_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
      EXPORTING NAME  = 'Accept'
                VALUE = 'text/xml'.

 CALL METHOD L_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING NAME  = '~request_method'
               VALUE = 'POST' .

 CALL METHOD L_HTTP_CLIENT->REQUEST->SET_CONTENT_TYPE
    EXPORTING CONTENT_TYPE  = 'text/xml' .

 "SETTING REQUEST DATA FOR 'POST' METHOD
 IF L_PARAMS_STRING IS NOT INITIAL.
   CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
     EXPORTING
         TEXT   = L_PARAMS_STRING
     IMPORTING
           BUFFER = L_PARAMS_XSTRING
     EXCEPTIONS
        FAILED = 1
        OTHERS = 2.

 CALL METHOD L_HTTP_CLIENT->REQUEST->SET_DATA
    EXPORTING DATA  = L_PARAMS_XSTRING  .
 ENDIF.

 "STEP-4 :  SEND HTTP REQUEST
  CALL METHOD L_HTTP_CLIENT->SEND
    EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE = 1
      HTTP_INVALID_STATE         = 2.

 "STEP-5 :  GET HTTP RESPONSE
    CALL METHOD L_HTTP_CLIENT->RECEIVE
      EXCEPTIONS
        HTTP_COMMUNICATION_FAILURE = 1
        HTTP_INVALID_STATE         = 2
        HTTP_PROCESSING_FAILED     = 3.

"STEP-6 : Read HTTP RETURN CODE
CALL METHOD L_HTTP_CLIENT->RESPONSE->GET_STATUS
    IMPORTING
      CODE = L_HTTP_STATUS_CODE
      REASON = L_STATUS_TEXT  .

WRITE: / 'HTTP_STATUS_CODE = ',
          L_HTTP_STATUS_CODE,
         / 'STATUS_TEXT = ',
         L_STATUS_TEXT .

"STEP-7 :  READ RESPONSE DATA
 CALL METHOD L_HTTP_CLIENT->RESPONSE->GET_CDATA
        RECEIVING DATA = L_RESULT .

"STEP-8 : CLOSE CONNECTION
CALL METHOD L_HTTP_CLIENT->CLOSE
  EXCEPTIONS
    HTTP_INVALID_STATE = 1
    OTHERS             = 2   .

"STEP-9 : PRINT OUTPUT TO FILE
CLEAR : L_XSTRING, L_OUT_TAB[].
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      TEXT   = L_RESULT
    IMPORTING
      BUFFER = L_XSTRING
    EXCEPTIONS
      FAILED = 1
      OTHERS = 2.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    BUFFER                = L_XSTRING
  TABLES
    BINARY_TAB            = L_OUT_TAB .

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
   FILENAME                        = 'C:AMEYHTTP_POST_OUTPUT.xml'
  TABLES
    DATA_TAB                        = L_OUT_TAB .

Also, following is the detailed link for use of HTTP_CLIENT class: -

http://help.sap.com/saphelp_nw70ehp1/helpdata/EN/1f/93163f9959a808e10000000a114084/content.htm

Also, in below link, you can ignore XI specific part and observe how its sending XML to external URL:-

(I know it describes call to SAP XI server's URL, but it can be used to call any URL)

http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/ae388f45-0901-0010-0f99-a76d785e3ccc

In addition to all above, following configs to be present at ABAP application server: -

1. The hostname used to URL should be present in SAP ABAP application server's 'hosts' file.

2. Security certificate (if available) for URL to be called must be installed in SAP ABAP application server.

Let me know if you achieve any progress with it...

Read only

0 Likes
3,446

Hi,

Can you please tel how the configuration steps mentioned above  have to be performed?

In addition to all above, following configs to be present at ABAP application server: -

1. The hostname used to URL should be present in SAP ABAP application server's 'hosts' file.

2. Security certificate (if available) for URL to be called must be installed in SAP ABAP application server.

Read only

0 Likes
3,446

To know more about how to create the instance for the class  CL_HTTP_CLIENT. The following link can be useful.

1.Link

regards

Supriyo

Read only

Former Member
0 Likes
3,446

When tried to use this solution to upload a file to Sharepoint, the following statement did not work:

CALL METHOD L_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD

    EXPORTING NAME  = '~request_method'

               VALUE = 'POST' .

I had to use:

CALL METHOD L_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD

    EXPORTING NAME  = '~request_method'

               VALUE = 'PUT' .

Also be aware that Sharepoint requires the full domain qualified name e.g. "domain\username" and not just "username".

My test program ended up as:

*&---------------------------------------------------------------------*

*& Report  ZTEMP_HTTP_XML

*&

*&---------------------------------------------------------------------*

*& This program uploads a file to a Sharepoint folder.

*&

*&---------------------------------------------------------------------*

REPORT  ztemp_http_xml.

PARAMETERS: p_user(20) TYPE c LOWER CASE.

PARAMETERS: p_pwd(20) TYPE c LOWER CASE.

DATA: l_url               TYPE                   string          ,

      l_params_string     TYPE                   string          ,

      l_http_client       TYPE REF TO            if_http_client  ,

      l_result            TYPE                   string          ,

      l_status_text       TYPE                   string          ,

      l_http_status_code  TYPE                   i               ,

      l_http_length       TYPE                   i               ,

      l_params_xstring    TYPE                   xstring         ,

      l_xstring           TYPE                   xstring         ,

      l_is_xml_table      TYPE STANDARD TABLE OF smum_xmltb      ,

      l_is_return         TYPE STANDARD TABLE OF bapiret2        ,

      l_out_tab           TYPE STANDARD TABLE OF tbl1024         ,

      l_user              TYPE string                            ,

      l_pwd               TYPE string                            .

l_user = p_user. "ensure prefixed with Sharepoint domain

l_pwd = p_pwd.   "Windows network/Sharepoint password

MOVE 'http://.../httptest.xml' TO l_url.

MOVE '<XML as string>' TO l_params_string.

*STEP-1 : CREATE HTTP CLIENT

CALL METHOD cl_http_client=>create_by_url

  EXPORTING

    url                = l_url

  IMPORTING

    client             = l_http_client

  EXCEPTIONS

    argument_not_found = 1

    plugin_not_active  = 2

    internal_error     = 3

    OTHERS             = 4.

* l_http_client->propertytype_logon_popup = l_http_client->co_disabled. "prevent pop-up if user auth fails

"STEP-2 :  AUTHENTICATE HTTP CLIENT

CALL METHOD l_http_client->authenticate

  EXPORTING

    username = l_user

    password = l_pwd.

"STEP-3 :  SET HTTP HEADERS

CALL METHOD l_http_client->request->set_header_field

  EXPORTING

    name  = '~request_method'

*   VALUE = 'POST' . "does not work for Sharepoint

    value = 'PUT'.   "use for Sharepoint

"STEP-4 :  SETTING REQUEST DATA FOR 'POST/PUT' METHOD

IF l_params_string IS NOT INITIAL.

  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

    EXPORTING

      text   = l_params_string

    IMPORTING

      buffer = l_params_xstring

    EXCEPTIONS

      failed = 1

      OTHERS = 2.

  CALL METHOD l_http_client->request->set_data

    EXPORTING

      data = l_params_xstring.

ENDIF.

"STEP-5 :  SEND HTTP REQUEST

CALL METHOD l_http_client->send

  EXCEPTIONS

    http_communication_failure = 1

    http_invalid_state         = 2.

"STEP-6 :  GET HTTP RESPONSE

CALL METHOD l_http_client->receive

  EXCEPTIONS

    http_communication_failure = 1

    http_invalid_state         = 2

    http_processing_failed     = 3.

"STEP-7 : Read HTTP RETURN CODE

CALL METHOD l_http_client->response->get_status

  IMPORTING

    code   = l_http_status_code

    reason = l_status_text.

WRITE: / 'HTTP_STATUS_CODE = ',

          l_http_status_code,

         / 'STATUS_TEXT = ',

         l_status_text .

"STEP-8 :  READ RESPONSE DATA

CALL METHOD l_http_client->response->get_cdata

  RECEIVING

    data = l_result.

"STEP-9 : CLOSE CONNECTION

CALL METHOD l_http_client->close

  EXCEPTIONS

    http_invalid_state = 1

    OTHERS             = 2.

"STEP-10 : PRINT OUTPUT TO FILE

CLEAR : l_xstring, l_out_tab[].

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

  EXPORTING

    text   = l_result

  IMPORTING

    buffer = l_xstring

  EXCEPTIONS

    failed = 1

    OTHERS = 2.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

  EXPORTING

    buffer     = l_xstring

  TABLES

    binary_tab = l_out_tab.

CALL FUNCTION 'GUI_DOWNLOAD'

  EXPORTING

    filename = 'C:\temp\POST_OUTPUT.xml'

  TABLES

    data_tab = l_out_tab.