Skip to Content

HTTP GET Method: Calling an external RESTful service from ABAP – HTTP Method GET

POST operation is almost equal to GET operation as my previous blog. Please refer my previous blog for more inline code description.

In POST operation we need to set a payload (JSON or XML) during the operation.

Create a RFC destination of type (HTTP Connections to External Server)



HTTP POST URL: with Header Authentication token

Request JSON: ‘{ “salesorder”:”25252525″, “type”:”Direct”}’

Expected Response JSON: ‘{ “token“:”CONFIRM7391797”}’


DATA: lo_http_client     TYPE REF TO if_http_client,
         lo_rest_client     TYPE REF TO cl_rest_http_client,
         lv_url             TYPE        string,
         http_status        TYPE        string,
         lv_body            TYPE        string.
      destination              = 'VENDAVO'    " Logical destination (specified in function call)
      client                   = lo_http_client    " HTTP Client Abstraction
      argument_not_found       = 1
      destination_not_found    = 2
      destination_no_authority = 3
      plugin_not_active        = 4
      internal_error           = 5
      OTHERS                   = 6
* If you are using cl_http_client=>create_by_url use this code to supress and pass your
 * HTTP basic authenication 
 *  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
 *  DATA l_username TYPE string.
 *  DATA l_password TYPE string.
 *  l_username = 'user'.
 *  l_password = 'password'.
 *  CALL METHOD lo_http_client->authenticate
 *      username = l_username
 *      password = l_password.
CREATE OBJECT lo_rest_client
       io_http_client = lo_http_client.
   lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_0 ).
   IF lo_http_client IS BOUND AND lo_rest_client IS BOUND.
     lv_url = 'salesorder'.
         request = lo_http_client->request    " HTTP Framework (iHTTP) HTTP Request
         uri     = lv_url                     " URI String (in the Form of /path?query-string)
     TYPES: BEGIN OF ty_json_req,
            salesorder TYPE string,
            type TYPE string,
            END OF ty_json_req.
     DATA: json_req TYPE ty_json_req.
     json_req-salesorder = '25252525'.
     json_req-type = 'Direct'.
DATA lr_json_serializer   TYPE REF TO cl_trex_json_serializer.
CREATE OBJECT lr_json_serializer  EXPORTING  data = json_req.
lr_json_serializer->serialize( ).
lv_body = lr_json_serializer->get_data( ).
* Or you can use this class as well
* lv_body = /ui2/cl_json=>serialize( data = json_req ).
* Converted JSON should look like this
* lv_body = '{ "salesorder":"25252525", "type":"Direct"}'.
     DATA: lo_json        TYPE REF TO cl_clb_parse_json,
           lo_response    TYPE REF TO if_rest_entity,
           lo_request     TYPE REF TO if_rest_entity,
           lo_sql         TYPE REF TO cx_sy_open_sql_db,
           status         TYPE  string,
           reason         TYPE  string,
           response       TYPE  string,
           content_length TYPE  string,
           location       TYPE  string,
           content_type   TYPE  string,
           lv_status      TYPE  i.
* Set Payload or body ( JSON or XML)
     lo_request = lo_rest_client->if_rest_client~create_request_entity( ).
     lo_request->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).
     lo_request->set_string_data( lv_body ).
CALL METHOD lo_rest_client->if_rest_client~set_request_header
         iv_name  = 'auth-token'
         iv_value = token number. "Set your header .
     lo_rest_client->if_rest_resource~post( lo_request ).
* Collect response
     lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
     http_status = lv_status = lo_response->get_header_field( '~status_code' ).
     reason = lo_response->get_header_field( '~status_reason' ).
     content_length = lo_response->get_header_field( 'content-length' ).
     location = lo_response->get_header_field( 'location' ).
     content_type = lo_response->get_header_field( 'content-type' ).
     response = lo_response->get_string_data( ).
     DATA lr_json_deserializer TYPE REF TO cl_trex_json_deserializer.
     TYPES: BEGIN OF ty_json_res,
            token TYPE string,
            END OF ty_json_res.
     DATA: json_res TYPE ty_json_res.
     CREATE OBJECT lr_json_deserializer.
     lr_json_deserializer->deserialize( EXPORTING json = response IMPORTING abap = json_res ).

To report this post you need to login first.


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

  1. Gaurav Sharma

    Hello ,

    I tried this code to call post method in SAP but getting error “Bad Request 400 “.

    Please suggest me how to resolve this problem.


    Gaurav Sharma

    1. Gaurav Sharma

      Hello ,

      Problem resolved , Issue with JSON data .

      I use this code to get Internal table data in JSON fromat .

      DATA lr_json_serializer   TYPE REF TO cl_trex_json_serializer.

      CREATE OBJECT lr_json_serializer  EXPORTING  data = json_req.

      lr_json_serializer->serialize( ).

      lv_body = lr_json_serializer->get_data( ).

      But Issue was its giving incomplete data like

      {Id : “4”,TYPE:”TEST”,DESCRIPTION:”TEST TYPE”,Hierarchy:”1″} then i corrected it manually with { “arguments”: {“Id” : 4,”TYPE”:”TEST”,”DESCRIPTION”:”TEST TYPE”,”Hierarchy”:1}} and its works .

      arguments is namspace for JSON.

      So please suggest me how to expected or complete Json format from ABAP.


      Gaurav Sharma


Leave a Reply