Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
52,715

Introduction

Now a days most of the API’s are in the form of Rest based services and the data that is retrieved from these services are in the form of JSON format. This tutorial will make you understand how to parse a JSON format data

Pre Requisites

                         The System has to be configured so that it can communicate with Rest based services. You can check whether the system is configured for consuming Rest based Services by going to transaction code STRUST. You should be able to see all the below mentioned configurations. If not, contact your BASIS for setting up the system in such a way.

       If the API you are going to consume has some Certificates. Then import them as well into the SSL.

Coding

                 

Create an RFC destination if needed and test it, whether it is pinging to the API that you have mentioned

data:
        lo_client         type ref to if_http_client,
        lv_result_url     type string,
        lv_bin            type xstring,
        lo_conv           type ref to cl_abap_conv_in_ce,
        lv_response       type string.

      if lv_matkt is not initial.

        cl_http_client=>create(
         exporting
           host    = ‘<api name>'
           service = '443'
           scheme  = 2
         importing
           client  = lo_client ).

        lo_client->request->set_method( if_http_request=>co_request_method_get ).


        concatenate
               'https://&lt;api name>.com/?'
               '&lt;param 1>=&lt;value 1>'
               '&lt;param 2>=&lt;value 2>’
               into
               lv_result_url respecting blanks.

        cl_http_utility=>set_request_uri( request = lo_client->request uri = lv_result_url ).
        lo_client->send( ).
        lo_client->receive( ).

        lv_bin = lo_client->response->get_data( ).

        lo_conv = cl_abap_conv_in_ce=>create( input = lv_bin ).
        lo_conv->read( importing data = lv_response ).

        lo_client->close( ).

        data: lv_matchcount_positive type zsample,
              lv_matchcount_negative type i,
              lv_startpos            type i,
              lv_endpos              type i.

        find '"seriesName":"PositiveSentiment","set":[' in lv_response match offset lv_startpos.
        find ']' in section offset lv_startpos of lv_response match offset lv_endpos .

        lv_startpos = lv_startpos + 40.
        lv_endpos = lv_endpos - lv_startpos.

        wa_netbase-category = 'Positive Sentiments'.
        wa_netbase-value    = lv_response+lv_startpos(lv_endpos).
        append wa_netbase to lt_output.

        clear: lv_response,
               lv_startpos,
               lv_endpos,
               wa_netbase.

       

       The above mentioned code mentions of how to call a API and then parse the data to get the desired output. First, you have to understand the output that come form the API. The output will have a certain format like, &lt;parameter name>:&lt;value>. The whole structure might differ based on the additional parameters you pass. So based on that you have to do string operations and functions to get the desired output. In my case the JSON format data has the parameter name as “positivesentiment’ and the values as ‘100’. So I used to search for the key word and then find the value that is present after the “:”.

5 Comments
byunki_jeon
Explorer
0 Kudos

Hi Alex.

I'm trying to use API which offer REST, Json format data structure.

When I tested  lv_bin variable is null.

I assume the reason why i can't get data is due to lo_client is null.

Would you explain what is purpose of call cl_http_client and if there's any pre requsition that i should check? For example, define a RFC destination in SM59 t-code.

 

 

Here's my RFC code.

 

data:

        lo_client         type ref to if_http_client,

        lv_result_url     type string,

        lv_bin            type xstring,

        lo_conv           type ref to cl_abap_conv_in_ce,

        lv_response       type string.

 

     cl_http_client=>create(

         exporting

           host    = 'invoice.zoho'

           service = '443'

           scheme  = 2

         importing

           client  = lo_client ).

 

lo_client->request->set_method( if_http_request=>co_request_method_get ).

 

concatenate

               'https://invoice.zoho.com/api/v3/organizations?'

               'authtoken=75ac81d5acb166073a57885a2ca9f1d9'

               into

               lv_result_url respecting blanks.

 

 

        cl_http_utility=>set_request_uri( request = lo_client->request uri = lv_result_url ).

 

        lo_client->send( ).

*        lo_client->receive( ).

 

        lv_bin = lo_client->response->get_data( ).

 

        lo_conv = cl_abap_conv_in_ce=>create( input = lv_bin ).

        lo_conv->read( importing data = lv_response ).

 

        lo_client->close( ).

 

your commnet would be really helpful to me.

Thanks in advance.

 

Best regards.

Jin Hyun Kang.

Former Member
0 Kudos

Below is what i did recently

 

REST based web service consumption in ABAP


The below example shows how we can consume an third party web service based on REST + XML protocol in SAP ABAP

 

 

The Package SREST is available with SAP_BASIS 7.31 and newer.

Documentation can be found here.

http://scn.sap.com/community/abap/connectivity/blog/2013/05/16/usage-of-the-abap-rest-library-sapbas...

 

Also Standard SAP help can be found in below link

http://help.sap.com/saphelp_nw74/helpdata/en/93/f28917057f47e78a2abd4a8cf0f7b9/content.htm?frameset=...

 

 

Below code shows steps involved in calling an external web service which is based on REST + XML using POST Method

This web service takes Registration number of the car as input and returns back all vehicle details in XML format.

In this example i will try to extract vehicle model code from the response XML string

 

 

The Third party web service provider will give you

Development end point URL example : http://xyzautosystems/getvehicleinfo

User ID and Password to access the web service

 

 

The below example uses XML for Request and response

 

 

DATA: lo_http_client TYPE REF TO if_http_client,

lo_rest_client TYPE REF TO if_rest_client,

lo_rest_entity TYPE REF TO if_rest_entity.

 

 

DATA: ls_match         TYPE match_result,

lv_offset_start  TYPE sy-tabix,

lv_offset_end    TYPE sy-tabix,

lv_string        TYPE string,

lv_model_code    TYPE char12,

lv_length        TYPE sy-tabix.

    

* Create HTTP Client

cl_http_client=>create_by_url(

  EXPORTING

url = 'http://xyzautosystems/getvehicleinfo '  "This is the Development end point URL

  IMPORTING

client = lo_http_client

  EXCEPTIONS

argument_not_found = 1

plugin_not_active  = 2

internal_error     = 3

OTHERS = 4 ).

IF sy-subrc = 0.

* Create REST Client

  CREATE OBJECT lo_rest_client TYPE cl_rest_http_client

EXPORTING

io_http_client = lo_http_client.

 

* Create REST entity Object

  lo_rest_entity = lo_rest_client->create_request_entity( ).

 

* Create Request XML data in a string form

  lv_string = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>'.

  CONCATENATE lv_string '<vmsmessage type="getvehicleinfo" version="1.0" number="1">' INTO lv_string.

  CONCATENATE lv_string '<credentials>' INTO lv_string.

  CONCATENATE lv_string '<username>user name</username>' INTO lv_string.

  CONCATENATE lv_string '<apikey>password</apikey>' INTO lv_string.

  CONCATENATE lv_string '</credentials>' INTO lv_string.

  CONCATENATE lv_string '<registrationnumber>CARREG</registrationnumber>'   INTO lv_string.   " This is the actual Input to web service

  CONCATENATE lv_string '<return>0</return>'  INTO lv_string.

  CONCATENATE lv_string '</dmsimessage>' INTO lv_string.

 

* Set the content format as XML

lo_rest_entity->set_content_type( if_rest_media_type=>gc_appl_xml ).

 

* Set Request XML data as entity content

  CALL METHOD lo_rest_entity->set_string_data

EXPORTING

iv_data = lv_string.

    

* POST request ( or call the web service )

lo_rest_client->post( lo_rest_entity ).

 

* Get Response entiry

  lo_rest_entity = lo_rest_client->get_response_entity( ).

 

* Get Respose XML as string

  lv_string = lo_rest_entity->get_string_data( ).

 

*  Now lv_string contains all the data in the form of Responce XML format

*  You can either do a call transformation or just do a simple search for the XML TAG for vehicle model code

  FIND '<model_code>'  IN lv_string RESULTS ls_match.

  IF sy-subrc = 0.

lv_offset_start = ls_match-offset + ls_match-length.

  ENDIF.

 

  FIND '</model_code>' IN lv_string RESULTS ls_match.

  IF sy-subrc = 0.

lv_offset_end = ls_match-offset.

  ENDIF.

 

  lv_length = lv_offset_end - lv_offset_start.

  lv_model_code = lv_string+lv_offset_start(lv_length).

 

 

  1. ENDIF.

 

 

Enjoy

Jojo Joseph

SAP ABAP consultant

*****Lets do it in ABAP , The most simple yet most powerful and robust coding language in the ERP world****

former_member214084
Participant
0 Kudos
Hi All

This post is very nice.

I am trying to call a 3rd party REST Service from SAP via call by URL  method when I am sending HTTP request getting error 404 access denied in return.

Could you re-direct me to point from where to start investigation to fix the problem and get ok message 200 in return.

link is something like :   http://ip address /api/TryInsert/111111112/SMO/

 
lakshmanjk
Explorer
0 Kudos
Hi Jin Hun Kang,

Please help with the similar issue reported in the below link.

 

https://answers.sap.com/questions/735732/calling-rest-api-get-method-from-sap-abap.html

 
lakshmanjk
Explorer
0 Kudos
Hi All,

Please help with the similar issue reported in the below link if you resolved it.

 

https://answers.sap.com/questions/735732/calling-rest-api-get-method-from-sap-abap.html

 

 
Labels in this area