Consuming external REST Service in sap abap with nested json body

Hi,

   In real time scenario we get the requirement to consume the rest based api  from other sap or non sap systems into sap and in this we need to pass json body also along with data.

   you can use this abap code for reference.

url :  ( this is dummy url )

https://restapitest.com/SAP/api/BPlan

we have to consume this url and in body the json we have pass from abap as sample given below

{

    "BP": {

"Billing_Plan_Number":" 0000023709",

        "Project_ID":"1000",

        "Tower":"A",

        "Billing_Plan_Type":"CA",

        "PayPlanItemsList":[

            {

                "SR_NO":"72",

                "Billing_date":"2018-01-08",

                "date_description":"BD01",

                "usage_milestone":"",

                "description":"Booking Day",

                "percentage":"10",

                "billing_value":"0",

                "currencType":"",

                "block":"02",

                "milestone_assing":"",

                "billing_rule":"1",

                "ZTERM":"BD01",

                "date_category":"",

                "billing_type":" CA ",

                "milestone_number":"",

                "ID_for_billing":"0"

            },

            {

                "SR_NO":"134",

                "Billing_date":"2019-01-30",

                "date_description":"",

                "usage_milestone":"BW01",

                "description":"Brick Work",

                "percentage":"5.00",

                "billing_value":"0",

                "currencType":"",

                "block":"02",

                "milestone_assing":"",

                "billing_rule":"1",

                "ZTERM":"BW01",

                "date_category":"",

                "billing_type":" CA ",

                "milestone_number":"12143",

                "ID_for_billing":"1"

            } ]      

            }

            }               

Now to pass  the data from abap into above body format , first we have to do the type declaration so the final structure will look like above json structure with header field and also body  here PayPlanItemsList

is the table and remaining fields are header level


*************** abap type declaration and data declaration  ****************  

types : begin of tp_bp_item,
          SR_NO(6),                        
          Billing_date(10),                 ":"2018-01-08",
          date_description(6),            
          usage_milestone(6),            
          description(50),                
          percentage(10),                  
          billing_value(17),
          currencType(10),                
          block(2),                       
          milestone_assing(1),             
          billing_rule(1),                 
          ZTERM(4),                        
          date_category(2),              
          billing_type(4),                 
          milestone_number(12),           
          ID_for_billing(1),                 
        end of tp_bp_item,

        begin of tp_bp_header,
          Billing_Plan_Number(10),           
          Project_ID(4),                      
          Tower(10),                         
          Billing_Plan_Type(10),             
          PayPlanItemsList type TABLE OF TP_BP_ITEM with DEFAULT KEY,
        end of tp_bp_header,

        BEGIN OF tp_bp,
          bp type TP_BP_HEADER,
        end of tp_bp.

data : gs_bp type tp_bp,

 ******************************************************************

with above type and data declaration our data structure is ready.

after filling the data into above gs_bp type we have to consume the webservice bypassing this data into json body

************

LV_SERVICE_URL  = |https://restapitest.com/SAP/api/BPlan|.

  CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
    EXPORTING
      URL                                = LV_SERVICE_URL
    IMPORTING
      CLIENT                         = LO_HTTP_CLIENT
    EXCEPTIONS
      ARGUMENT_NOT_FOUND = 1
      PLUGIN_NOT_ACTIVE  = 2
      INTERNAL_ERROR     = 3
      OTHERS                       = 4.

*set http method POST

  CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_METHOD(
    IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ).

*set protocol version

  LO_HTTP_CLIENT->REQUEST->SET_VERSION(    IF_HTTP_REQUEST=>CO_PROTOCOL_VERSION_1_0 ).

  CALL METHOD LO_HTTP_CLIENT->REQUEST->IF_HTTP_ENTITY~SET_FORMFIELD_ENCODING
    EXPORTING
      FORMFIELD_ENCODING = CL_HTTP_REQUEST=>IF_HTTP_ENTITY~CO_ENCODING_RAW.

*content type

  CALL METHOD LO_HTTP_CLIENT->REQUEST->IF_HTTP_ENTITY~SET_CONTENT_TYPE
    EXPORTING
      CONTENT_TYPE = IF_REST_MEDIA_TYPE=>GC_APPL_JSON.


  CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD

    EXPORTING

      NAME  = 'Accept'

      VALUE = '*/*'.


  CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING
      NAME  = 'Accept-Encoding'
      VALUE = 'gzip, deflate, br'.


*get data
  CLEAR : LV_SAP_RES, LV_RESPONSE.
    LO_HTTP_REQUEST = LO_HTTP_CLIENT->REQUEST.


*append data

    LO_HTTP_REQUEST->APPEND_CDATA( DATA = gs_json ).


    CALL METHOD LO_HTTP_CLIENT->SEND(

      EXPORTING

        TIMEOUT                    = 15

      EXCEPTIONS

        HTTP_COMMUNICATION_FAILURE = 1

        HTTP_INVALID_STATE         = 2

        HTTP_PROCESSING_FAILED     = 3

        OTHERS                     = 4 ).


    CALL METHOD LO_HTTP_CLIENT->RECEIVE(

      EXCEPTIONS

        HTTP_COMMUNICATION_FAILURE = 1

        HTTP_INVALID_STATE         = 2

        HTTP_PROCESSING_FAILED     = 3

        OTHERS                     = 4 ).


*response

    DATA(LO_RES) = LO_HTTP_CLIENT->RESPONSE.

    LV_RESPONSE = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).

    write : LV_RESPONSE.

************************** 


for complete code refer below code lines

*&---------------------------------------------------------------------*

*& Report  ZBPLAN_TRANSFER

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT ZBPLAN_TRANSFER.

tables : zsd_bp.

types : begin of tp_bp_item,
          SR_NO(6),                        "":"72",
          Billing_date(10),                 ":"2018-01-08",
          date_description(6),             ":"BD01",
          usage_milestone(6),              ":"",
          description(50),                 ":"Booking Day",
          percentage(10),                  ":"10",
          billing_value(17),":"0",
          currencType(10),                 ":"",
          block(2),                        ":"02",
          milestone_assing(1),             ":"",
          billing_rule(1),                 ":"1",
          ZTERM(4),                        ":"BD01",
          date_category(2),                ":"",
          billing_type(4),                 ":"CA",
          milestone_number(12),            ":"",
          ID_for_billing(1),                 ":"0"
        end of tp_bp_item,


        begin of tp_bp_header,
          Billing_Plan_Number(10),            ":" 0000023709",
          Project_ID(4),                      ":"4786",
          Tower(10),                          ":"A",
          Billing_Plan_Type(10),              ":"Non Scheme",
          PayPlanItemsList type TABLE OF TP_BP_ITEM with DEFAULT KEY,
        end of tp_bp_header,


        BEGIN OF tp_bp,
          bp type TP_BP_HEADER,
        end of tp_bp.


data : gt_sd_bp type standard table of zsd_bp,
       gs_sd_bp type zsd_bp,
       gt_fplt type standard table of fplt,
       gs_fplt type fplt,
       gt_t433t type standard table of t433t,
       gs_t433t type t433t,
       gs_bp type tp_bp,
       gt_bp_item type STANDARD TABLE OF TP_BP_ITEM,
       gs_bp_item type TP_BP_ITEM.


  DATA : LV_SERVICE_URL TYPE STRING,
               LV_SAP_RES     TYPE STRING,
               L_RESPUESTA    TYPE STRING,
               LV_RESPONSE    TYPE STRING,
              gs_json type string,
             GS_HTTP_VP     TYPE IHTTPNVP,
             GT_HTTP_VP     TYPE TIHTTPNVP.

DATA : LO_HTTP_CLIENT  TYPE REF TO IF_HTTP_CLIENT,

       LO_HTTP_REQUEST TYPE REF TO IF_HTTP_ENTITY.


selection-screen begin of block b1 with frame title text-001.

  select-options : s_plant for zsd_bp-werks,

                   s_tower for zsd_bp-tower,

                   s_date for zsd_bp-CPUDT.

selection-screen skip.

parameters : c_init as checkbox.

selection-screen end of block b1.


start-of-selection.

     perform get_data.

     perform post_data.



end-of-selection.

*&---------------------------------------------------------------------*

*&      Form  GET_DATA

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*----------------------------------------------------------------------*

FORM GET_DATA.


   if C_INIT is NOT INITIAL.


   select * from zsd_bp into CORRESPONDING FIELDS OF TABLE GT_SD_BP

   WHERE WERKS in S_PLANT

     and TOWER in S_TOWER.


  else.


   select * from zsd_bp into CORRESPONDING FIELDS OF TABLE GT_SD_BP

   WHERE WERKS in S_PLANT

     and TOWER in S_TOWER

     and CPUDT in S_DATE.


  endif.


  delete GT_SD_BP WHERE FPLNR is INITIAL.

  sort GT_SD_BP by WERKS TOWER.

  delete ADJACENT DUPLICATES FROM GT_SD_BP COMPARING werks tower.


  if GT_SD_BP[] is NOT INITIAL.


  select * from fplt into CORRESPONDING FIELDS OF TABLE GT_FPLT

  FOR ALL ENTRIES IN GT_SD_BP WHERE fplnr = GT_SD_BP-fplnr.


  select * from t433t into CORRESPONDING FIELDS OF TABLE GT_T433T WHERE SPRAS = sy-LANGU.


  sort GT_SD_BP by FPLNR FPLTR.


  endif.


ENDFORM.

*&---------------------------------------------------------------------*

*&      Form  POST_DATA

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*----------------------------------------------------------------------*

FORM POST_DATA.


   loop at gt_sd_bp into gs_sd_bp.


   clear : gt_bp_item, gs_bp_item, gs_bp.

   loop at gt_fplt into gs_fplt where fplnr = gs_sd_bp-fplnr.

   clear : GS_BP_ITEM.


   GS_BP_ITEM-SR_NO                     = gs_fplt-fpltr.

*   GS_BP_ITEM-Billing_date              = gs_fplt-AFDAT.

   CONCATENATE gs_fplt-afdat+0(4) '-' gs_fplt-afdat+4(2) '-' gs_fplt-afdat+6(2) into GS_BP_ITEM-Billing_date.

   GS_BP_ITEM-date_description          = gs_fplt-TETXT.

   GS_BP_ITEM-usage_milestone           = gs_fplt-MLBEZ.

*   GS_BP_ITEM-description

    GS_BP_ITEM-percentage               = gs_fplt-FPROZ.

    CONDENSE GS_BP_ITEM-percentage.

    GS_BP_ITEM-billing_value             = gs_fplt-FAKWR.

    CONDENSE GS_BP_ITEM-BILLING_VALUE.

*   GS_BP_ITEM-currencType

*   GS_BP_ITEM-block

*   GS_BP_ITEM-milestone_assing

*   GS_BP_ITEM-billing_rule

   GS_BP_ITEM-ZTERM                     = gs_fplt-ZTERM.

*   GS_BP_ITEM-date_category

*   GS_BP_ITEM-billing_type

*   GS_BP_ITEM-milestone_number

*   GS_BP_ITEM-ID_for_billing


   append gs_bp_item to gt_bp_item.


   clear : gs_fplt.

   endloop.


   clear : gs_bp.

   gs_bp-BP-Billing_Plan_Number     = GS_SD_BP-fplnr.

   gs_bp-BP-Project_ID              = GS_SD_BP-WERKS.

   gs_bp-BP-Tower                   = GS_SD_BP-TOWER.

   gs_bp-BP-Billing_Plan_Type       = GS_SD_BP-FPART.

   gs_bp-BP-PayPlanItemsList[]      = GT_BP_ITEM[].



   clear : gs_json.


   /UI2/CL_JSON=>SERIALIZE(

     EXPORTING

       DATA        =  gs_bp   " Data to serialize

*       COMPRESS    = ABAP_true    " Skip empty elements

*       NAME        =     " Object name

*       PRETTY_NAME = /ui2/cl_json=>pretty_mode-camel_case    " Pretty Print property names

*       TYPE_DESCR  =     " Data descriptor

     RECEIVING

       R_JSON      =   gs_json  " JSON string

   ).


   perform json_naming.


  LV_SERVICE_URL  = |https://restapitest.com/SAP/api/BPlan|.

  CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL

    EXPORTING

      URL                = LV_SERVICE_URL

    IMPORTING

      CLIENT             = LO_HTTP_CLIENT

    EXCEPTIONS

      ARGUMENT_NOT_FOUND = 1

      PLUGIN_NOT_ACTIVE  = 2

      INTERNAL_ERROR     = 3

      OTHERS             = 4.


*set http method POST

  CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_METHOD(

    IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ).

*set protocol version

  LO_HTTP_CLIENT->REQUEST->SET_VERSION( IF_HTTP_REQUEST=>CO_PROTOCOL_VERSION_1_0 ).


  CALL METHOD LO_HTTP_CLIENT->REQUEST->IF_HTTP_ENTITY~SET_FORMFIELD_ENCODING

    EXPORTING

      FORMFIELD_ENCODING = CL_HTTP_REQUEST=>IF_HTTP_ENTITY~CO_ENCODING_RAW.


*content type

  CALL METHOD LO_HTTP_CLIENT->REQUEST->IF_HTTP_ENTITY~SET_CONTENT_TYPE

    EXPORTING

      CONTENT_TYPE = IF_REST_MEDIA_TYPE=>GC_APPL_JSON.


  CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD

    EXPORTING

      NAME  = 'Accept'

      VALUE = '*/*'.


  CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD

    EXPORTING

      NAME  = 'Accept-Encoding'

      VALUE = 'gzip, deflate, br'.


*get data

  CLEAR : LV_SAP_RES, LV_RESPONSE.


    LO_HTTP_REQUEST = LO_HTTP_CLIENT->REQUEST.


*append data

    LO_HTTP_REQUEST->APPEND_CDATA( DATA = gs_json ).


    CALL METHOD LO_HTTP_CLIENT->SEND(

      EXPORTING

        TIMEOUT                    = 15

      EXCEPTIONS

        HTTP_COMMUNICATION_FAILURE = 1

        HTTP_INVALID_STATE         = 2

        HTTP_PROCESSING_FAILED     = 3

        OTHERS                     = 4 ).


    CALL METHOD LO_HTTP_CLIENT->RECEIVE(

      EXCEPTIONS

        HTTP_COMMUNICATION_FAILURE = 1

        HTTP_INVALID_STATE         = 2

        HTTP_PROCESSING_FAILED     = 3

        OTHERS                     = 4 ).


*response

    DATA(LO_RES) = LO_HTTP_CLIENT->RESPONSE.


    LV_RESPONSE = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).


    write : LV_RESPONSE.


   clear : gs_sd_bp.

   endloop.



ENDFORM.

*&---------------------------------------------------------------------*

*&      Form  JSON_NAMING

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*----------------------------------------------------------------------*

FORM JSON_NAMING.


  replace all OCCURRENCES OF 'BP' in gs_json with 'BP'.

  replace all OCCURRENCES OF 'BILLING_PLAN_NUMBER' in gs_json with 'Billing_Plan_Number'.

  replace all OCCURRENCES OF 'PROJECT_ID' in gs_json with 'Project_ID'.

  replace all OCCURRENCES OF 'TOWER' in gs_json with 'Tower'.

  replace all OCCURRENCES OF 'BILLING_PLAN_TYPE' in gs_json with 'Billing_Plan_Type'.

  replace all OCCURRENCES OF 'PAYPLANITEMSLIST' in gs_json with 'PayPlanItemsList'.

  replace all OCCURRENCES OF 'SR_NO' in gs_json with 'SR_NO'.

  replace all OCCURRENCES OF 'BILLING_DATE' in gs_json with 'Billing_date'.

  replace all OCCURRENCES OF 'DATE_DESCRIPTION' in gs_json with 'date_description'.

  replace all OCCURRENCES OF 'USAGE_MILESTONE' in gs_json with 'usage_milestone'.

  replace all OCCURRENCES OF 'DESCRIPTION' in gs_json with 'description'.

  replace all OCCURRENCES OF 'PERCENTAGE' in gs_json with 'percentage'.

  replace all OCCURRENCES OF 'BILLING_VALUE' in gs_json with 'billing_value'.

  replace all OCCURRENCES OF 'CURRENCTYPE' in gs_json with 'currencType'.

  replace all OCCURRENCES OF 'BLOCK' in gs_json with 'block'.

  replace all OCCURRENCES OF 'MILESTONE_ASSING' in gs_json with 'milestone_assing'.

  replace all OCCURRENCES OF 'BILLING_RULE' in gs_json with 'billing_rule'.

  replace all OCCURRENCES OF 'ZTERM' in gs_json with 'ZTERM'.

  replace all OCCURRENCES OF 'DATE_CATEGORY' in gs_json with 'date_category'.

  replace all OCCURRENCES OF 'BILLING_TYPE' in gs_json with 'billing_type'.

  replace all OCCURRENCES OF 'MILESTONE_NUMBER' in gs_json with 'milestone_number'.

  replace all OCCURRENCES OF 'ID_FOR_BILLING' in gs_json with 'ID_for_billing'.

ENDFORM.

Post a Comment

0 Comments

Total Pageviews