cancel
Showing results for 
Search instead for 
Did you mean: 

Calling a web service in external system from SRM

Former Member
0 Kudos

Hi folks,

A web service is created in the external system and I need to access this web service from a BADI. Can you tell me how can I call this web service (the external system is giving me a URL) and how I'll get a return. Please let me know in detail.

Thanks,

Prem

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Prem,

Hi. You can call the service via HTTP protocol. Pass them values (SET_DATA), and receive a response (GET_DATA), via xml/html.

In your code you would need to create the xml data to pass them, and evaluate the returned xml.

Process...

Data setup

1) Create the XML to send them

Working with the external service

2) Open the HTTP connection

2a) cl_http_client=>create_by_url (IF_HTTP_CLIENT)

2b) lr_client->authenticate

3) Call the to send them the XML

3a) lr_client->request->set_data

3b) lr_client->send

4) Call the lr_client->receive to return the response

5) Close the connection lr_client->close

Data evaluate

6) Evaluation the returned XML and process.

Hope this helps

Cheers

Rob

Code example below.. (There are loads of SAP examples depending on which release you are on).

----


  • Process the call to the HTTP client - logic copied from RSHTML01 *

----


  • Open IF_HTTP_CLIENT

call method cl_http_client=>create_by_url

exporting

url = l_url

importing

client = lr_client

exceptions

argument_not_found = 1

plugin_not_active = 2

internal_error = 3

others = 4.

if sy-subrc <> 0.

message id sy-msgid type sy-msgty number sy-msgno

with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4

raising oops.

endif.

  • Authenticate the user

if not g_int_type-usr is initial.

move: g_int_type-usr to l_user,

g_int_type-password to l_password.

call method lr_client->authenticate

exporting

username = l_user

password = l_password.

endif.

  • Allow for Cookies

lr_client->propertytype_accept_cookie = lr_client->co_enabled.

  • Set the server protocol

select single gsval into l_server_protocol

from z77s0

where grpid = c_grpid

and semid = c_server_protocol.

if sy-subrc eq 0

and not l_server_protocol is initial.

move l_server_protocol to l_st_server_protocol.

call method lr_client->request->set_header_field

exporting

name = '~server_protocol'

value = l_st_server_protocol.

endif.

  • Send out the XML

  • Set body to XML data

lr_client->request->set_data( g_xxml ).

save_xml( i_role = cl_xml_document=>c_role_oreq ).

l_request_length = xstrlen( g_xxml ).

  • If Data is sent through then we need certain flags set

lr_client->request->set_header_field(

name = 'Content-Type'

value = zcl_tem_bsp=>c_xml_content ).

call method lr_client->request->set_header_field

exporting

name = '~request_method'

value = 'POST'.

  • Set length of string to the header fields

if not l_request_length is initial.

move l_request_length to l_st_request_length.

lr_client->request->set_header_field(

name = 'content-length'

value = l_st_request_length ).

endif.

  • Send the request

call method lr_client->send

exceptions

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

http_invalid_timeout = 4

others = 5.

check_for_error 'Send'.

  • Receive the response

call method lr_client->receive

exceptions

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

others = 4.

check_for_error 'Receive'.

  • Determined returned XML or HTML

g_xxml = lr_client->response->get_data( ).

  • Determine the header fields for failure validation

if lr_client->response->get_header_field( '~status_code' )

between 200 and 299.

save_xml( i_role = cl_xml_document=>c_role_ires ).

else.

l_status_code =

lr_client->response->get_header_field( '~status_code' ).

l_descript_1 =

lr_client->response->get_header_field( 'error' ).

l_descript_2 =

lr_client->response->get_header_field( 'errortext' ).

Former Member
0 Kudos

Hello,

Could you please send us the whole example with all the code? or give a link to find another example to get an xml from an URI.

Thanks a lot.