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

Consuming Web service

anub
Participant
0 Likes
2,894

Hi Experts,

I have a problem while consuming the Web Service in ABAP. My requirement is that I have a Web Service in Java that I need to consume in SAP.

I have used CALL TRANSFORMATION to convert the data in SAP to xml output.

{CALL TRANSFORMATION ZHR_TM_SYNC}

{SOURCE interfacefm = t_interface}

{RESULT XML xml_result.}

Here t_interface is the SAP structure and xml_result is my xml output.

Next I call the method cl_http_client=>create_by_url.

I have declared the Java portal WS URL in w_string.

My question is should I pass w_string or xml_result to the method cl_http_client=>create_by_url.

How will I pass my sap data, which is in xml_result, to the Java portal?

When I pass xml_result, I do not get any output when I run the report. But when I put the WS URL I get some output, without any data that I want to pass to the Java Portal.

I have searched in sdn and in google but everywhere I found that the Transformation is called after the method call, and the transformation is from xml to abap.

Thanks

Anu.

Hi Experts,

I have a problem while consuming the Web Service in ABAP. My requirement is that I have a Web Service in Java that I need to consume in SAP.

I have used CALL TRANSFORMATION to convert the data in SAP to xml output.

{CALL TRANSFORMATION ZHR_TM_SYNC}

{SOURCE interfacefm = t_interface}

{RESULT XML xml_result.}

Here t_interface is the SAP structure and xml_result is my xml output.

Next I call the method cl_http_client=>create_by_url.

I have declared the Java portal WS URL in w_string.

My question is should I pass w_string or xml_result to the method cl_http_client=>create_by_url.

How will I pass my sap data, which is in xml_result, to the Java portal?

When I pass xml_result, I do not get any output when I run the report. But when I put the WS URL I get some output, without any data that I want to pass to the Java Portal.

I have searched in sdn and in google but everywhere I found that the Transformation is called after the method call, and the transformation is from xml to abap.

Thanks

Anu.

14 REPLIES 14
Read only

Former Member
0 Likes
2,071

hey,

the web service is from a 3rd party system?

do you have SAP netweaver PI in your project?

regards,

roi grosfeld

Read only

0 Likes
2,071

The Webservice is from the Java side and we are not using PI.

Thanks

Anu.

Read only

Former Member
0 Likes
2,071

Hi Anu,

You don't need to convert your data in XML format. You just need to pass the data to method as you pass to a normal method in SAP.

You can do standalone testing of webservice from SE80 for your method.

Regards,

Ramnivas

Read only

Former Member
0 Likes
2,071

Anu,

To consume an external WS in SAP, you need to get the WSDL (Web Service Definition Language) from your external JAVA WS. That WSDL defines the methods, attributes, etc of the JAVA WS to SAP.

SAP needs that WSDL to build a Proxy Class that will be used as the interface technology between SAP and the JAVA WS.

Here is a starting point for you:

http://help.sap.com/saphelp_nw04/helpdata/en/81/845f3c31727d59e10000000a114084/frameset.htm

Read only

0 Likes
2,071

Hi John,

Is it possible to achieve the same through ABAP coding?

Thanks,

Anu.

Read only

matt
Active Contributor
0 Likes
2,071

It's possible but difficult. Get the WSDL, generate a consumer proxy from it, start programming. It's easy and standard. Why would you want to do it any other way?

Read only

0 Likes
2,071

Thanks for the reply.

But in my project the client is providing a simple http post Web Service and not SOAP based one.

So there is no WSDL file for it.

How can I consume the Web Service in this case?

Any alternative?

Read only

0 Likes
2,071

Hi,

Go through the below thread you should be able to accomplish what you are looking for, in case you have any specific question update the post with your query.

Regards,

Chen

Read only

0 Likes
2,071

Thanks Chen for your reply.

When I run my report I get the following output.

######<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">##<html>##<head>## <title>Error</title>##

<link rel="stylesheet" href="/dummy_sap/css/productstyle.css" type="text/css" />##</head>##<body>####</body>##</html>######

I find there is some error in the output. Also there is no data that is transferred. What could be wrong?

Thanks

Anu.

Read only

0 Likes
2,071

Hi,

When I debug the program I found after the send method the program flow goes to if_abap_runtime method.

{CALL METHOD http_client->send}

{ http_communication_failure = 1}

{ http_invalid_state = 2.}

{method if_abap_runtime~get_runtime by kernel module ab_kmGetRuntimeHR.}

{endmethod.}

Please guide what would be the reason for getting if_abap_runtime.

Thanks

Anu.

Read only

0 Likes
2,071

Hi Anu,

I find there is some error in the output. Also there is no data that is transferred. What could be wrong?

Good news is that it is able to invoke the HTTP service on the remote system. Possibly, the input might be wrong, make sure that the input you are sending from SAP is as the HTTP Service is expecting it...say for example if you are sending the date, is it as the Service is expecting(YYYYMMDD or MMDDYYYY)...does the input need to be in lower/upper case etc.

CALL METHOD http_client->send}

{ http_communication_failure = 1}

{ http_invalid_state = 2.}

{method if_abap_runtime~get_runtime by kernel module ab_kmGetRuntimeHR.}

{endmethod.}

Please guide what would be the reason for getting if_abap_runtime.

Does HTTP_CLIENT->SEND throw an exception?, if not then i guess it is ok as the call to "GET_RUNTIME" could be part of the standard code.

Post your code, if it is throwing an exception.

Regards,

Chen

Read only

0 Likes
2,071

Check the output of the transformation at the call transformation statement in the debugging mode and see everything is as required or not.

Edited by: Sandeep S on Jun 29, 2011 4:51 PM

Read only

0 Likes
2,071

Thanks Chen for your help.

I found that the file I needed to upload should be a multipart/form-data.

I have made the changes accordingly as shown below and now my report is able to consume the WS.

DATA: xml_result TYPE string,

w_string TYPE string.

DATA: CONTENT TYPE STRING.

DATA: rlength TYPE i.

Data: part type ref to if_http_entity.

DATA: X1 TYPE STRING, X2 TYPE STRING.

*// Call transformation, ls_interface contains SAP data and xml_result is the xml output

CALL TRANSFORMATION ZHR_REP

SOURCE employees = lS_interface

RESULT XML xml_result.

*// URL

w_string = 'http://xxx.com/xx_sap/createCandidate'.

*// UTF-16 replaced by UTF-8

SPLIT XML_RESULT AT 'utf-16' INTO X1 X2.

CLEAR XML_RESULT.

CONCATENATE X1 'utf-8' X2 INTO XML_RESULT.

CLEAR :rlength .

rlength = STRLEN( xml_result ).

CALL METHOD cl_http_client=>create_by_url

EXPORTING

url = w_string

IMPORTING

client = http_client

EXCEPTIONS

argument_not_found = 1

plugin_not_active = 2

internal_error = 3

OTHERS = 4.

  • set http method POST

call method http_client->request->set_method(

if_http_request=>CO_REQUEST_METHOD_POST ).

  • set protocol version

http_client->request->set_version(

if_http_request=>co_protocol_version_1_0 ).

  • content type

CALL METHOD HTTP_CLIENT->REQUEST->IF_HTTP_ENTITY~SET_CONTENT_TYPE

EXPORTING

CONTENT_TYPE = 'multipart/form-data'.

CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = 'Accept'

VALUE = 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8'.

CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = 'Accept-Language'

VALUE = 'en-gb,en;q=0.5'.

CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = 'Accept-Charset'

VALUE = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'.

CALL METHOD HTTP_CLIENT->REQUEST->IF_HTTP_ENTITY~SET_FORMFIELD_ENCODING

EXPORTING

FORMFIELD_ENCODING = CL_HTTP_REQUEST=>IF_HTTP_ENTITY~CO_ENCODING_RAW .

part = HTTP_CLIENT->REQUEST->IF_HTTP_ENTITY~ADD_MULTIPART( ).

CALL METHOD part->set_header_field

EXPORTING

name = 'content-disposition'

value = 'form-data; name="File"; filename="candidate_v1.xml";'.

CALL METHOD part->set_content_type

EXPORTING

content_type = 'text/xml'.

CALL METHOD part->set_cdata

EXPORTING

data = xml_result

offset = 0

length = rlength.

call method http_client->send

EXPORTING

timeout = 8

EXCEPTIONS

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

others = 4.

CALL METHOD http_client->receive

EXCEPTIONS

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3.

content = http_client->response->get_cdata( ).

http_client->close( ).

Thanks

Anu.

Edited by: Anu Abraham on Jul 7, 2011 10:48 AM

Read only

0 Likes
2,071

Hi Anu,

I have same requirement but in this i need to pass json string to webservice .i have connected to webservice through test mail but i failed in sending input  to that.i got the error stating that request is syntactically incorrect.i didn't use multipart/form data.what is the purpose of it .i just send the required json string to if_http_client~request->set_cdata.

Please guide me how to proceed.

Thanks,

Anil.