Skip to Content

There have been many requests to know how to consume webservice from ABAP. I wanted to share a rudimentary sample written in ABAP of how this can be done. I am not sure whether this is a right approach though it does the job. Inspiration for this approach was from DJ Adams weblog Forget SOAP – build real web services with the ICF

For this exercise I have used a free webservice for getting Airport details by country name located at http://www.webservicex.net/airport.asmx?op=GetAirportInformationByCountry   (please go to this URL to look at the SOAP message)

 

Points to note:

  • The output internal table (outtab)  is designed after looking at the SOAP response. This can also be done dynamically using ABAP XSLT and CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
  • SAP system Used: 4.7/6.2 WAS

 

ABAP Report  Y_CONSUME_WEBSERVICEco_disabled.
 
wf_user = user .
wf_password = password .
 

  1. proxy server authentication


CALL METHOD http_client->authenticate
  EXPORTING
    proxy_authentication = ‘X’
    username             = wf_user
    password             = wf_password.
 
 
CALL METHOD http_client->request->set_header_field
  EXPORTING
    name  = ‘~request_method’
    value = ‘POST’.
 
CALL METHOD http_client->request->set_header_field
  EXPORTING
    name  = ‘~server_protocol’
    value = ‘HTTP/1.1’.
 
CALL METHOD http_client->request->set_header_field
  EXPORTING
    name  = ‘~request_uri’
    value = ‘/airport.asmx’.
 
CALL METHOD http_client->request->set_header_field
  EXPORTING
    name  = ‘Content-Type’
    value = ‘text/xml; charset=utf-8’.
 
CALL METHOD http_client->request->set_header_field
  EXPORTING
    name  = ‘Content-Length’
    value = txlen.
 
CALL METHOD http_client->request->set_header_field
  EXPORTING
    name  = ‘SOAPAction’
    value = ‘http://www.webserviceX.NET/GetAirportInformationByCountry‘.
 
CALL METHOD http_client->request->set_cdata
  EXPORTING
    data   = wf_string
    offset = 0
    length = rlength.
 
CALL METHOD http_client->send
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2.
 
CALL METHOD http_client->receive
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3.
 
CLEAR wf_string1 .
wf_string1 = http_client->response->get_cdata( ).
 
REPLACE ALL OCCURRENCES OF
    ‘<‘ IN wf_string1 WITH ‘<‘ .
 
REPLACE ALL OCCURRENCES OF
 ‘>’ IN wf_string1 WITH ‘>’ .
 
REPLACE ALL OCCURRENCES OF
 ‘xmlns=’ IN wf_string1 WITH ‘xmlns:xsl=’ .
 
TRY .
    CALL TRANSFORMATION (`Y_AIRPORT_XML2ABAP`)
            SOURCE XML wf_string1
            RESULT     outtab = outtab.
  CATCH cx_xslt_exception INTO xslt_err.
 
    DATA: s TYPE string.
    s = xslt_err->get_text( ).
    WRITE: ‘: ‘, s.
    STOP.
 
ENDTRY .
 
Loop at outtab .
Write:/ outtab .
Endloop .

XSLT program  Y_AIRPORT_XML2ABAP.

 

+Path =>   Txn SE80 – > Edit Object (shift F5) ->more->XSLT program</p><p class=”MsoNormal”> </p><p class=”MsoNormal”><textarea cols=”85″ name=”S1″ rows=”12″><xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0″>
 
  <xsl:strip-space elements=”*”/>
 
  <xsl:output indent=”yes”/>
 
 
  <xsl:template match=”NewDataSet”>
    <asx:abap xmlns:asx=”http://www.sap.com/abapxml” version=”1.0″>
      <asx:values>
        <OUTTAB>
          <xsl:for-each select=”Table”>
 
            <OUTTAB1>
              <AIRPORTCODE>
                <xsl:value-of select=”AirportCode”/>
              </AIRPORTCODE>
              <CITYOFAIRPORTNAME>
                <xsl:value-of select=”CityOrAirportName”/>
              </CITYOFAIRPORTNAME>
              <COUNTRY>
                <xsl:value-of select=”Country”/>
              </COUNTRY>
              <COUNTRYABBRIVATION>
                <xsl:value-of select=”CountryAbbrviation”/>
              </COUNTRYABBRIVATION>
              <COUNTRYCODE>
                <xsl:value-of select=”CountryCode”/>
              </COUNTRYCODE>
              <GMTOFFSET>
                <xsl:value-of select=”GMTOffset”/>
              </GMTOFFSET>
              <RUNWAYLENGTHFEET>
                <xsl:value-of select=”RunwayLengthFeet”/>
              </RUNWAYLENGTHFEET>
              <RUNWAYELEVATIONFEET>
                <xsl:value-of select=”RunwayElevationFeet”/>
              </RUNWAYELEVATIONFEET>
              <LATITUDEDEGREE>
                <xsl:value-of select=”LatitudeDegree”/>
              </LATITUDEDEGREE>
              <LATITUDEMINUTE>
                <xsl:value-of select=”LatitudeMinute”/>
              </LATITUDEMINUTE>
              <LATITUDESECOND>
                <xsl:value-of select=”LatitudeSecond”/>
              </LATITUDESECOND>
              <LATITUDENPEERS>
                <xsl:value-of select=”LatitudeNpeerS”/>
              </LATITUDENPEERS>
              <LONGITUDEDEGREE>
                <xsl:value-of select=”LongitudeDegree”/>
              </LONGITUDEDEGREE>
              <LONGITUDEMINUTE>
                <xsl:value-of select=”LongitudeMinute”/>
              </LONGITUDEMINUTE>
              <LONGITUDESECONDS>
                <xsl:value-of select=”LongitudeSeconds”/>
              </LONGITUDESECONDS>
              <LONGITUDEEPERW>
                <xsl:value-of select=”LongitudeEperW”/>
              </LONGITUDEEPERW>
            </OUTTAB1>
          </xsl:for-each>
        </OUTTAB>
 
      </asx:values>
    </asx:abap>
 
 
 
  </xsl:template>
 
</xsl:stylesheet>
</textarea></p><p style=”margin-left: 0.25in” class=”MsoNormal”> </p>

To report this post you need to login first.

40 Comments

You must be Logged on to comment or reply to a post.

  1. Tony John
    this blog is a perfect example for anyone who wants to start using SOAP over HTTP in ABAP

    Thank you very much for making it so simple

    Tony

    (0) 
    1. Anton Wenzelhuemer
      hi there,
      this blog was great for the time (and releases)written and it is still great today in demystifying webservices because of its illustrativ approach showing that a soap message isn’t much more than an ordinary HTPP message containing some special header fields and a specific kind of payload.

      Nevertheless, IMHO it’s not really state of the art today and for contemporary releases. Today we have wizards creating soap clients from the WSDL directly therefore being more accurate by taking into account soap errors, handling HTTP error conditions and considering data types (a little). Moreover XML bodies do not have to be parsed ‘manually’.

      I suggest to have a look at the webservices section here on SDN and find a number of actual examples.

      But again I want to state that it was a great blog at it’s time and it still is for didactical reasons today.

      regards,
      anton

      (0) 
      1. Durairaj Athavan Raja Post author
        Hi Anton,

        Thanks for taking time to put your thoughts. You are right, as you have mentioned the time and releases were of critical factor when it was developed. In the later blogs i have used WAS6.40 features(Send SMS to India from ABAP)

        for those who are interested in the wizard based WAS 6.40 approache can check out this great blog by Thomas Jung.
        BSP a Developer’s Journal Part XIV – Consuming WebServices with ABAP

        Regards
        Raja

        (0) 
  2. Chaitanya Kovvuri
    Hi,

    I tried to use the code to execute the program, however in the http_client->receive call i receive sy-subrc = 1,  I am not using any user id pwd when executing the program.

    Could you tell me the configuration steps required before program execution. I checked Proxy settings/port/transaction SICF etc but am not sure what exactly needs to be done.

    Thank you

    (0) 
  3. Chaitanya Kovvuri
    Hi,

    I checked with our basis team and they mentioned that we did not need to use proxy authentication, however we kept getting this error “HTTPIO_PLG_NO_MPI_INIT” and I checked in OSS and found note 876932 which needs a patch to be applied.Hope the patch helps.Will keep SDN updated. 

    (0) 
  4. Prasad Mangina
    Hi,
      I tried to test this example on a 4.6C system (no WAS).

    I am getting compilation errors :
    DATA: xslt_err TYPE REF TO cx_xslt_exception .
    Error is : type cx_xslt_exception is unknown.

    Can you suggest if we need to be on 4.7 & WAS 6.XX. Or do we need to install any add-ons.

    rdgs
    Prasad

    (0) 
    1. Durairaj Athavan Raja Post author
      it wont work in 4.6C.

      to make it work in 4.6C you need use http_get function module instead of cl_http_client class and also you cannot use CALL TRANFORMATION

      Regards
      Raja

      (0) 
    2. Prasad Mangina
      Hi Raja,
        Thanks for your reply. We tried it in 4.7 & it works fine.

      Now we are planning to use this in our ECC 6.0 system. But it is not working, do you know if we need to make any chnages to make it work.

      rdgs
      Prasad

      (0) 
  5. Ken Bol
    Copied sample program into our development environment and ran pgm. Received communication error, checked SICF, proxy is not active. We are running 4.7 620, not sure what to try next, please advise.

    Thanks,
    Ken Bol
    Brady Corporation
    ken_bol@bradycorp.com

    (0) 
  6. Soma Achari
    Hello ,
         In my WSDL I have a Hierarchy so that the repeating values in an table is not sent everytime in the input and output. I am able to consume the Webservice but when trying to send the request I am having issues with the data types etc.
    Input : Example: Order Number,sold to party
                          |—–Item number
                          | —–Product
                                 |—– Qty
                                
    Output:
            Product
               |——-Attributes of the product
                   Message
    Is  there a place I can get a sample of the above scenario.

    Thanks

    (0) 
  7. Prasad Mangina
    Hi Raja,
    Thanks for info. We tried it in 4.7 & it works fine.

    Now we are planning to use this in our ECC 6.0 system. But it is not working, do you know if we need to make any chnages to make it work.

    rdgs
    Prasad

    (0) 
      1. Prasad Mangina
        Hi Raja,
          Thanks for the reply.

        I will review the other blog as well.

        Your example we tested in 4.7 & it worked fine. How ever in ECC 6.0 the following statement is not brining any response.

        CLEAR wf_string1 .
        wf_string1 = http_client->response->get_cdata( ).

        So there could be some issue in the earlier coding also some where.

        Is there any checks that we can do if this is a SAP system issue (Do we need to activate any services etc on the SAP system ) or the ABAP program issue.

        rdgs
        Prasad

        (0) 
        1. Prasad Mangina
          Hi Raja,
            Although I am in SAP (ABAP – more than 8 yrs), I am not much into BSP’s etc.
          What is the basic difference between the solution given in your blog & the newer one you suggested to review.

          rdgs
          Prasad

          (0) 
          1. Durairaj Athavan Raja Post author
            in this example i am hardcoding soap message whereas in the other weblog i have linked, you created what is called client proxy from the wsdl (which is basically a ABAP class)

            and that abap class/method can be called to consume the webservice.

            Regards
            Raja

            (0) 
  8. Chaitanya Kovvuri
    Hi Raja,

    I get the error in the subject line, I tried to check for the other dynamic options to perform transformation in 6.2 but could not get clear understanding from the documentation.

    If you have a good weblog in mind do mention the same.

    Thank you.

    (0) 
  9. KP J
    Hi,
    FIrst off, your weblog was a great source of information for calling webservices from ABAP.
    I really learned a lot.

    I copy pasted your code but ran into a small problem.
    I am getting the error ‘:  ABAP XML formatting error in XML node of type “element”, name: “{http://www.sap.com/abapxml}abap”
    when i execute ‘Y_CONSUME_WEBSERVICE’.

    And in the debugger I can see after the statement
    wf_string1 = http_client->response->get_cdata( )
    all the right data in the string.

    But it fails at
    ‘CALL TRANSFORMATION (`Z_AIRPORT_XML2ABAP`)
                  SOURCE XML wf_string1
                  RESULT     outtab = outtab.

    and I get the output as I have pasted on the top.

    Can you tell what could be wrong here?

    Thanks a lot.

    (0) 
    1. Durairaj Athavan Raja Post author
      the error is to do with the XSLT. can you copy paste the code which wf_string1 has and the xslt code and mail it to me , will see whats going wrong. you can find my email id in my business card.

      Raja

      (0) 
    1. Tony Raimo
      I’m getting the same error.  Raja, do you have a resolution to this problem?  wf_string1 has data.  I can see the results in xml format.  However, it won’t translate to ABAP.

      BTW, Great BLOG.  It helped me a lot.

      Thank you,

      (0) 
      1. Philipp Egger
        I ran into the same problem. When analysing the SOAP Response you will see, that the inner tags are html encoded < instead of < (> instead of >)

        The following lines in the code should replace that:

        REPLACE ALL OCCURRENCES OF
        ‘<‘ IN wf_string1 WITH ‘<‘ .

        REPLACE ALL OCCURRENCES OF
        ‘>’ IN wf_string1 WITH ‘>’ .

        Unfortunately, it does not show correctly on an HTML page, because the right version is:

        REPLACE ALL OCCURRENCES OF
        ‘<‘ IN wf_string1 WITH ‘<‘ .

        REPLACE ALL OCCURRENCES OF
        ‘>’ IN wf_string1 WITH ‘>’ .

        Greets,

        Phil

        (0) 
  10. Markus Tradt
    Hi Raja, thanks for thes valuable blog.

    You write:
    In this example the SOAP message is hard coded, better way would be to get the WSDL and parse it using ABAP XSLT program and build the SOAP message at runtime.

    Would you please give some hints, how to do that. Unfortunately I have to call a service from a 4.7 system.
    Thanks
    Markus

    (0) 
    1. Durairaj Athavan Raja Post author
      you would need a such a parser only if you are going to build a abap proxy generation system. if its one off (or few WS calls) then i would suggest to manually build the SOAP message. Moreover, from WAS 6.40 SAP provides mechanism to build the client proxy. Wo its going to be waste of time building one.

      Regards
      Raja

      (0) 
  11. Yan Zhitnitsky
    Hi Raja,

    Thanks for you example, I tested it and it works fine! However, in your example, you have SOAP message hard coded, however I will have to call WSDL service (one file).
    Do you have an example how to call WSDL service from ABAP?

    Thanks in advance!

    Regards,
    Yan

    (0) 
    1. Durairaj Athavan Raja Post author
      This example was written for old version of R/3. if you are on WAS 6.40 or above, you should use the standard way of creating client proxy from WSDL for consuming the webservice. You can find quiet a few examples on this in the webblog area here.

      Raja

      (0) 
  12. Alfredo Gomez Ripoll
    Hi Raja
    Thank´s a lot for this weblog , I´ve learned a lot and it´s very clear and simple .
    I need to do something like this because of problems of compatibility between the version of SAP at work (640) and my Web service supplier ; problem is that client proxy generation wizard is not able to  generate the methods ,ending with errors .
    I´d like to adapt your example to my own proyect but I´m facing 3 problems :

    – 1) In variable wf_string you hardcode a SOAP message ¿how do i know the message that I have to hardcode in my own wf_string variable?
    – 2) You are using method CALL METHOD http_client->request->set_header_field ¿ how do i know what header fields should i SET for my own needs?
    – 3) Related to transformation , SE80 give us 2 possibilities:S , Simple transformation X , XSLT programm ; I´m using XSLT program , according to your blog ,question is ¿there´re some guidelines to write the code i´ve to include , for example , I guess , in the wsdl file?

    Thank you again

    (0) 
    1. Durairaj Athavan Raja Post author
      1] I used a free tool called webservicestudio to analyze the wsdl and come up with request SOAP message format.

      2]Its purely dependent on the service definition of your WS

      3]I am not sure i understood the question in full, but you could use whatever you are comfortable with. i always use XSLT.

      (0) 
  13. Seb Seb
    Hello,

    Thanks for this blog.
    I copy the program and transformation, but when i execute the program, a message is displayed : ‘No valid source context supplied’.

    Can you help me ?

    (0) 
  14. Fernando Ariel Villa
    Hi Raja, thanks a lot for posting all this. I’ve been browsing around and there’s almost no information on this subject.

    I need some assistance anyway, and maybe you can enlighten me about my problem.

    I’ve copied both program and transformatioin, but when running the report I constantly get this error about non valid source.

    This is the begining of what I have in wf_string1 before the transformation:

    ##
    Do you have any ideas of what could be wrong here?
    Is there any customizing needed before the code you posted works?

    Thanks once again,

    Fernando

    (0) 
  15. Durairaj Athavan Raja Post author
    The reply button didnt work, thus posting as a new message.

    Can you check whether the webservice works from browser? from the sample response, you had posted, looks like the ws is not returning proper xml.

    (0) 
    1. Fernando Ariel Villa
      Hi Raja, thanks for your answer.
      As you said, I checked the service and it was unavailable. Once it was restablished, the program worked ok.
      Now I’m having some dificulties with the transformation.
      I get a message saying that there’s an error in a field of type text, name ‘{}’. Any suggestions about this?
      By the way, would you have any information you could share about consuming web services running on secure http?

      Thank you once again.
      Best regards,

      Fernando

      (0) 

Leave a Reply