You are on page 1of 15

ABAP-Based HTTP Client for Messages to SAP XI

Applies To:
SAP NW 04 XI 3.0 and SAP R/3 4.6C and above

Summary
In the SAP Exchange Infrastructure (XI), the HTTP adapter is native to the Integration Server (IS). A sender HTTP Adapter need not be configured in XI. HTTP messages can be sent to the native HTTP adapter of XI. There is a browser-based tool already available for this purpose. This article describes an ABAP-based tool for acting as the sender HTTP adapter. By: Bhanumurthy Thirumala Company: Intelligroup, Inc. Date: 07 February 2006

Table of Contents
Applies To:........................................................................................................................................1 Summary ..........................................................................................................................................1 Table of Contents .............................................................................................................................1 Introduction.......................................................................................................................................2 Description of the ABAP program for HTTP Client ..........................................................................2 Selection Screen of the program ..................................................................................................2 Explanation of the Selection Screen Fields ..................................................................................4 Runtime behavior..........................................................................................................................5 Program Constraints.....................................................................................................................6 Source Code of the ABAP program for HTTP Client .......................................................................7 Texts needed for the ABAP program for HTTP Client ...................................................................14 Selection Texts ...........................................................................................................................14 Text Elements .............................................................................................................................14 Browser-based HTTP Client...........................................................................................................15 Author Bio.......................................................................................................................................15 Disclaimer & Liability Notice ...........................................................................................................15

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 1

ABAP-Based HTTP Client for Messages to SAP XI

Introduction
The HTTP adapter can be used for sending a payload to XI without SOAP envelope. It is native to the Integration Server (IS) in XI. Consequently, there is no need to configure a sender HTTP Adapter in XI. Browser-based tools are already available for sending HTTP messages to the native HTTP adapter of XI. However, an ABAP-based tool can have certain advantages over the browser-based tool, if the sending system is based on SAP. This article describes an ABAP-based tool for acting as the sender HTTP adapter. There may be valid business reasons for the SAP-based sending system to use a HTTP adapter, such as: o o o Ability to send messages to several SAP systems in the same format Minimum efforts in configuration and maintenance Automate the sending process

Some advantages of using an ABAP-based HTTP Client as compared to the browser-based tool: o o o o o o Save different variants of selection screen for different receivers Possibility to Schedule automated jobs to send messages from XML files Provision to integrate the application into the business events of the back-end ERP system In-line Text Editor that can be used for editing the Payload Helpful in testing any Receiver Adapter Can be enhanced to include logging the errors to a file and any custom validations on the Payload contents and communication data

Description of the ABAP program for HTTP Client


This ABAP Program is an executable stand-alone report program, with a selection screen. It does not have any other dependent components. The only requirement is that the SAP environment in which this is executed should have the function module HTTP2_POST. The reason for using this function module HTTP2_POST and not the Class CL_HTTP_CLIENT is that this tool should be useful for older versions of SAP. The Class CL_HTTP_CLIENT has been undergoing some revisions long after the function module HTTP2_POST became available. If needed, the code can be modified to use the Class CL_HTTP_CLIENT instead. Selection Screen of the program Here is how the selection screen of the program appears:

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 2

ABAP-Based HTTP Client for Messages to SAP XI

Variants can be created and used as needed for each receiver. For example:

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 3

ABAP-Based HTTP Client for Messages to SAP XI

Explanation of the Selection Screen Fields Here is an explanation of the fields: Field Integration Server Host&Port Quality of Service Description The XI Integration Servers <Host>:<Port> Allowed values: o BE (Best Effort) o EO (Exactly Once) o EOIO (Exactly Once In Order) SAP Client of the XI system User to access the XI system client o Defaults to XIAPPLUER o If different, the user should have the same roles as XIAPPLUER Password for the user access to the XI system client Logon language for the user access to XI system client, Defaults to DE Sender Party, Optional

Client User

Password Language Party

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 4

ABAP-Based HTTP Client for Messages to SAP XI


Scheme Agency Service Interface Namespace Interface Payload from File Name of File Sender Scheme, Optional Sender Agency, Optional Sender Service Sender Interface Namespace Sender Interface Option to Upload the Payload from a File. The following three fields are relevant for this option File Name that contains the Payload. o Payload should have the XML tags o Payload should include this statement at the top: <?xml version="1.0" encoding="UTF-8"?> o Mandatory if the option Payload from File is selected. o Has a Search Help option that works based on the following options Option to upload the Payload File from Local Computer Option to upload the Payload File from the SAP Application Server Option to Upload the Payload from a File. o When selected, brings up a Text Editor for Entering / Editing / Pasting the Payload Text. o Text is saved as an internal table that has a text field 72 characters wide. This does not truncate any text. o When chosen, this option blanks out the File Name if entered

File on Local Computer File on Application Server Insert Payload as Text

If the option Insert Payload as Text is chosen, a Text editor appears as follows, where the payload can be entered, edited or pasted.

Runtime behavior When executed successfully, the results of the program run appear as in the following example. This example uses an Asynchronous call (QoS = EO). For Synchronous calls (QoS = BE), the results will be reported as in the internal table GT_RESPONSE_ENTITY_BODY.

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 5

ABAP-Based HTTP Client for Messages to SAP XI

When executed with errors, the results of the program run appear as in the following example. This example uses an Asynchronous call (QoS = EO), with a wrong Sender Service, resulting in errors during Receiver Determination in XI.

Program Constraints o When uploading the payload from a file, the line width has a limit of 1024 characters. Any characters in a line after position 1024 will be truncated. The password is not encrypted on the selection screen, for easier administration. If needed, code can be modified to encrypt this.

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 6

ABAP-Based HTTP Client for Messages to SAP XI


Source Code of the ABAP program for HTTP Client
Here is the source code of the ABAP Program:
*&---------------------------------------------------------------------* report Z_HTTP_CLIENT_FOR_XI line-size 132 line-count 65. *---------------------------------------------------------------------* *Data declaration *---------------------------------------------------------------------* tables SSCRFIELDS. data: begin of GS_PAYLOAD, TEXT(72) type C, end of GS_PAYLOAD, GT_PAYLOAD like standard table of GS_PAYLOAD, begin of GS_STRING, STR(1024) type C, end of GS_STRING, GT_STRING like standard table of GS_STRING, G_STR type STRING, begin of GS_LINE, LINE(132) type C, end of GS_LINE, G_QUERY(1024) type C. data: G_STATUS_CODE(72) type C, G_STATUS_TEXT(1024) type C, G_REQUEST_ENTITY_BODY_LENGTH type I, GT_RESPONSE_ENTITY_BODY like table of GS_LINE, GT_REQUEST_HEADERS like table of GS_LINE, GT_RESPONSE_HEADERS like table of GS_LINE. data: G_UCOMM type SY-UCOMM. *---------------------------------------------------------------------* *Selection Screen *---------------------------------------------------------------------* *HTTP Client for sending a message to XI Sender HTTP Adapter selection-screen begin of block HTTP_CLIENT with frame title TEXT-S01. selection-screen skip 1. *Header Parameters selection-screen begin of block HEADER_INFO with frame title TEXT-S02. parameters: P_SRVHST(255) type C lower case obligatory, " Integration Server URL with Host and Port P_QOS(4) type C, " Quality of Service *Logon information P_CLIENT type MANDT obligatory, " Integration Server SAP Client P_USER type SY-UNAME obligatory default 'XIAPPLUSER', " User P_PASSWD(20) type C lower case obligatory, " Password P_LANGU type SPRAS obligatory default 'DE'. " Logon language selection-screen end of block HEADER_INFO.

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 7

ABAP-Based HTTP Client for Messages to SAP XI


*Sender information selection-screen begin of block SENDER_INFO with frame title TEXT-S03. parameters: P_PARTY(60) P_SCHEME(120) P_AGENCY(120) P_SERVIC(60) P_NMSPAC(60) P_INTERF(60)

type type type type type

case, " Sender Party case, " Sender Scheme case, " Sender Agency case obligatory, " Sender Service case obligatory, " Sender Interface Namespace type C lower case obligatory. " Sender Interface

C C C C C

lower lower lower lower lower

selection-screen end of block SENDER_INFO. * Payload selection-screen begin of block PAYLOAD with frame title TEXT-S04. selection-screen begin of line. parameters: P_PAYFIL radiobutton group PAYL default 'X' user-command PAYL. selection-screen comment 3(20) TEXT-S05. selection-screen position 28. parameters: P_PAYTXT radiobutton group PAYL. selection-screen comment 31(25) TEXT-S06. selection-screen end of line. * Payload from File parameters: P_FILE type RLGRAP-FILENAME modif id FIL, " File P_LCL radiobutton group FILE default 'X' modif id " File P_APP radiobutton group FILE modif id FIL. " File selection-screen end of block PAYLOAD. selection-screen end of block HTTP_CLIENT. *---------------------------------------------------------------------* at selection-screen. *---------------------------------------------------------------------* G_UCOMM = SY-UCOMM. if G_UCOMM = 'PAYL'. if P_PAYTXT = 'X'. editor-call for GT_PAYLOAD title 'Payload Text'(001) backup into GT_PAYLOAD. clear: P_FILE. endif. endif. *---------------------------------------------------------------------* at selection-screen on value-request for P_FILE. *---------------------------------------------------------------------* perform FILENAME_HELP using P_FILE. *---------------------------------------------------------------------* at selection-screen output. *---------------------------------------------------------------------* if G_UCOMM = 'PAYL'.

Name FIL, on PC on App server

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 8

ABAP-Based HTTP Client for Messages to SAP XI


if P_PAYTXT = 'X'. loop at screen. if SCREEN-GROUP1 = 'FIL'. SCREEN-ACTIVE = '0'. SCREEN-INVISIBLE = '1'. modify screen. endif. endloop. endif. endif. *---------------------------------------------------------------------* start-of-selection. *---------------------------------------------------------------------* if P_PAYFIL is not initial and P_FILE is initial. message I499(SY) with 'Enter a File Name'(002). stop. endif. *If there is a file name, ignore the Payload uploaded as Text and read *it from the file name. if P_FILE is not initial. clear GT_PAYLOAD. * Upload the payload from file perform PAYLOAD_FROM_FILE tables GT_STRING using P_APP P_FILE. call function 'CONVERT_TABLE_TO_STRING' exporting I_TABLINE_LENGTH = 1024 importing E_STRING = G_STR tables IT_TABLE = GT_STRING. call function 'CONVERT_STRING_TO_TABLE' exporting I_STRING = G_STR I_TABLINE_LENGTH = 72 tables ET_TABLE = GT_PAYLOAD. endif. if GT_PAYLOAD is initial. message I499(SY) with 'Payload is empty'(003). stop. endif. *Build the Query string clear G_QUERY. concatenate P_SRVHST '/sap/xi/adapter_plain?' 'namespace=' P_NMSPAC '&interface=' P_INTERF '&service=' P_SERVIC '&party=' P_PARTY '&agency=' P_AGENCY '&scheme=' P_SCHEME '&QOS=' P_QOS '&sap-user=' P_USER

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 9

ABAP-Based HTTP Client for Messages to SAP XI


'&sap-password=' P_PASSWD '&sap-client=' P_CLIENT '&sap-language=' P_LANGU into G_QUERY. describe table GT_PAYLOAD lines SY-TFILL. G_REQUEST_ENTITY_BODY_LENGTH = SY-TFILL * 72. GS_LINE-LINE = 'content-type: text/xml; charset=utf-8'. append GS_LINE to GT_REQUEST_HEADERS. GS_LINE-LINE = G_REQUEST_ENTITY_BODY_LENGTH. shift GS_LINE-LINE left deleting leading SPACE. concatenate 'content-length:' GS_LINE-LINE into GS_LINE-LINE separated by SPACE. append GS_LINE to GT_REQUEST_HEADERS. call function 'HTTP2_POST' exporting ABSOLUTE_URI REQUEST_ENTITY_BODY_LENGTH RFC_DESTINATION USER PASSWORD BLANKSTOCRLF NO_LOGON importing STATUS_CODE STATUS_TEXT RESPONSE_ENTITY_BODY_LENGTH tables REQUEST_ENTITY_BODY RESPONSE_ENTITY_BODY RESPONSE_HEADERS REQUEST_HEADERS exceptions CONNECT_FAILED TIMEOUT INTERNAL_ERROR TCPIP_ERROR SYSTEM_FAILURE COMMUNICATION_FAILURE others

* * * *

= G_QUERY = G_REQUEST_ENTITY_BODY_LENGTH = = = = = '' = G_STATUS_CODE = G_STATUS_TEXT = = = = = = = = = = = = GT_PAYLOAD GT_RESPONSE_ENTITY_BODY GT_RESPONSE_HEADERS GT_REQUEST_HEADERS

1 2 3 4 5 6 7.

if SY-SUBRC <> 0. message id SY-MSGID type SY-MSGTY number SY-MSGNO with SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. else. write:/ 'Response from HTTP Post:'(004) color 1. uline. skip 1. write:/ 'Status Code:'(005) color 1, 20 G_STATUS_CODE, / 'Status Text:'(006) color 1, 20 G_STATUS_TEXT. uline. if GT_RESPONSE_HEADERS is not initial. skip 1. write:/ 'Response Header:'(007) color 1. uline.

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 10

ABAP-Based HTTP Client for Messages to SAP XI


loop at GT_RESPONSE_HEADERS into GS_LINE. write:/ GS_LINE-LINE. endloop. endif. if GT_RESPONSE_ENTITY_BODY is not initial. skip 1. uline. write:/ 'Response Body:'(008) color 1. loop at GT_RESPONSE_ENTITY_BODY into GS_LINE. write:/ GS_LINE-LINE. endloop. endif. endif. *&---------------------------------------------------------------------* *& Form PAYLOAD_FROM_FILE *&---------------------------------------------------------------------* * To upload the Payload from File into an internal table *----------------------------------------------------------------------* form PAYLOAD_FROM_FILE tables LT_PAYLOAD using L_APP type ANY L_FILENAME type RLGRAP-FILENAME. * Read the if L_APP data : L_FILE file from Presentation server is initial. L_FILE type STRING. = L_FILENAME.

call function 'GUI_UPLOAD' exporting FILENAME FILETYPE has_field_separator tables DATA_TAB exceptions FILE_OPEN_ERROR FILE_READ_ERROR NO_BATCH GUI_REFUSE_FILETRANSFER INVALID_TYPE NO_AUTHORITY UNKNOWN_ERROR BAD_DATA_FORMAT HEADER_NOT_ALLOWED SEPARATOR_NOT_ALLOWED HEADER_TOO_LONG UNKNOWN_DP_ERROR ACCESS_DENIED DP_OUT_OF_MEMORY DISK_FULL DP_TIMEOUT others

= L_FILE = 'ASC' = = LT_PAYLOAD = = = = = = = = = = = = = = = = = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17.

if SY-SUBRC <> 0. message I499(SY) with 'File upload failed'(009) L_FILENAME. stop. endif.

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 11

ABAP-Based HTTP Client for Messages to SAP XI

* Read the file from Application server else. open dataset L_FILENAME for input in text mode encoding default. if SY-SUBRC = 0. do. read dataset L_FILENAME into LT_PAYLOAD. if SY-SUBRC = 0. append LT_PAYLOAD. clear LT_PAYLOAD. else. exit. endif. enddo. close dataset L_FILENAME. else. message I499(SY) with 'File upload failed'(009) L_FILENAME. stop. endif. endif. endform. "PAYLOAD_FROM_FILE

*&---------------------------------------------------------------------* *& Form FILENAME_HELP *&---------------------------------------------------------------------* * Search Help for File name *----------------------------------------------------------------------* form FILENAME_HELP using L_FILE type RLGRAP-FILENAME. data: L_FIELDNAME type DYNPREAD-FIELDNAME, L_APP type CHAR01, LS_FIELDLIST type DYNPREAD, LT_FIELDLIST type table of DYNPREAD. *Read the screen to get the Application Server indicator LS_FIELDLIST-FIELDNAME = 'P_APP'. append LS_FIELDLIST to LT_FIELDLIST. LS_FIELDLIST-FIELDNAME = 'P_FILE'. append LS_FIELDLIST to LT_FIELDLIST. call function 'DYNP_VALUES_READ' exporting DYNAME = SY-CPROG DYNUMB = SY-DYNNR tables DYNPFIELDS = LT_FIELDLIST exceptions INVALID_ABAPWORKAREA = 1 INVALID_DYNPROFIELD = 2 INVALID_DYNPRONAME = 3 INVALID_DYNPRONUMMER = 4 INVALID_REQUEST = 5 NO_FIELDDESCRIPTION = 6 INVALID_PARAMETER = 7

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 12

ABAP-Based HTTP Client for Messages to SAP XI


UNDEFIND_ERROR DOUBLE_CONVERSION STEPL_NOT_FOUND others = = = = 8 9 10 11.

if SY-SUBRC <> 0. message I499(SY) with 'Selection Screen could not be read'(010). elseif not LT_FIELDLIST[] is initial. read table LT_FIELDLIST into LS_FIELDLIST with key FIELDNAME = 'P_APP'. if SY-SUBRC = 0. move LS_FIELDLIST-FIELDVALUE to L_APP. endif. read table LT_FIELDLIST into LS_FIELDLIST with key FIELDNAME = 'P_FILE'. if SY-SUBRC = 0. move LS_FIELDLIST-FIELDVALUE to L_FILE. endif. endif. *File Name search if L_APP = 'X'. data: L_SERVER type MSXXLIST-NAME, L_PATH type DXFIELDS-LONGPATH. L_PATH = L_FILE. * Derive the associated Application Server name call function 'FIND_DB_APPLICATION_SERVER' importing SERVERNAME = L_SERVER exceptions NO_APPLICATION_SERVER = 1 others = 2. if SY-SUBRC <> 0. message I499(SY) with 'No application server name defined'(012). endif. call function 'F4_DXFILENAME_TOPRECURSION' exporting I_LOCATION_FLAG = 'X' I_SERVER = L_SERVER I_PATH = L_PATH FILEMASK = '*.*' FILEOPERATION = 'R' importing O_LOCATION_FLAG = O_SERVER = O_PATH = L_PATH ABEND_FLAG = exceptions RFC_ERROR = 1 ERROR_WITH_GUI = 2 others = 3 . if SY-SUBRC <> 0. message I499(SY) with 'File name search help failed.'(011).

* * *

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 13

ABAP-Based HTTP Client for Messages to SAP XI


else. L_FILE = L_PATH. endif. else. L_FIELDNAME = 'P_FILE'. call function 'F4_FILENAME' exporting PROGRAM_NAME = SYST-CPROG DYNPRO_NUMBER = SYST-DYNNR FIELD_NAME = L_FIELDNAME importing FILE_NAME = L_FILE. endif. endform. " FILENAME_HELP

Texts needed for the ABAP program for HTTP Client


Following Selection Texts and Text Elements are needed for the Program. If the tool is to appear in a language other than English, the necessary translations need to be done for these texts. Selection Texts
P_AGENCY P_APP P_CLIENT P_FILE P_INTERF P_LANGU P_LCL P_NMSPAC P_PARTY P_PASSWD P_PAYFIL P_PAYTXT P_QOS P_SCHEME P_SERVIC P_SRVHST P_USER Agency File on Application Server Client Name of File Interface Language Key File on Local Computer Interface Namespace Party Password Payload from File Payload as Text Quality of Service Scheme Service Integration Server Host&Port User

Text Elements
001 002 003 004 005 006 Payload Text Enter a File Name Payload is empty Response from HTTP Post: Status Code: Status Text:

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 14

ABAP-Based HTTP Client for Messages to SAP XI


007 008 009 010 011 012 S01 S02 S03 S04 S05 S06 Response Header: Response Body: File upload failed Selection Screen could not be read File name search help failed. No application server name defined HTTP Client for sending a message to XI Sender HTTP Adapter Header Parameters Sender Parameters Payload: Can be inserted as Text or Uploaded from a File Payload from File Insert Payload as Text

Browser-based HTTP Client


If you are interested in a Browser-based HTTP Client, here are a few links where the corresponding HTML code can be obtained: SDN Forum - 1 <https://www.sdn.sap.com/irj/sdn/thread?forumID=44&threadID=39601> SDN Forum - 2 <https://www.sdn.sap.com/irj/sdn/message?messageID=266750>

Author Bio

Bhanumurthy Thirumala is a SAP Technical Consultant with 7 years of SAP experience, currently working with Intelligroup as Principal Consultant. He is also a SAP certified XI consultant.

Disclaimer & Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.

2006 SAP AG

The SAP Developer Network: http://sdn.sap.com 15