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 REST API with CL_HTTP_CLIENT in ABAP

Former Member
0 Likes
7,400

Hi all,


I am trying to consume the demo Service Desk plus REST API with CL_HTTP_CLIENT in ABAP.

Demo Service Desk plus trail version and installed in my local PC.(Application run on localhost).

I got help document from Service Desk plus to consuming REST API and URL format as below.


http://<servername>:<port number>/sdpapi/request/

http://localhost:8080/sdpapi/request/

  1. The operation name "ADD_REQUEST" should be sent as a "POST attribute" with key "OPERATION_NAME"
  2. The technician key should be sent as a "POST attribute" with key "TECHNICIAN_KEY"
  3. Input is an XML string sent as "POST attribute" with key "INPUT_DATA". 


Based on the above document, I have prepared my URL below : URL:


http://localhost:8080/sdpapi/request/?OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=A2D2DEFC-C3BB-4F17-A...<Operation><Details><requester>Howard Stern</requester><subject>Help</subject><status>1</status><description>Everything is broken</description></Details></Operation>


When  running the above URL in browse it is working fine (Please see the attached jpg ).




But i am facing problem when try to consum in ABAP.  This due to API in localhost  ?


Error message : ( host: proxy, service: 8080 failed (NIEHOST_UNKNOWN)#RM-T37, U19433 )


Code Snap


  lv_service = 'http://localhost:8080/sdpapi/request/?OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=A2D2DEFC-C3BB-4F17-A...&INPUT_DATA=<Operation><Details><requester>Howard Stern</requester><subject>Help!</subject><description>Everything is broken</description></Details></Operation>'.
  

   cl_http_client=>create_by_url(
     EXPORTING
       url                = lv_service
     IMPORTING
       client             = lo_http_client
     EXCEPTIONS
       argument_not_found = 1
       plugin_not_active  = 2
       internal_error     = 3
       OTHERS             = 4 ).

   lo_http_client->send(
     EXCEPTIONS
       http_communication_failure = 1
       http_invalid_state         = 2 ).

   lo_http_client->receive(
     EXCEPTIONS
       http_communication_failure = 1
       http_invalid_state         = 2
       http_processing_failed     = 3 ).

   "Prepare XML structure
   CLEAR lv_result .
   lv_result = lo_http_client->response->get_cdata( ).
   lo_ixml = cl_ixml=>create( ).
   lo_streamfactory = lo_ixml->create_stream_factory( ).
   lo_istream = lo_streamfactory->create_istream_string(
                                    lv_result ).
   lo_document = lo_ixml->create_document( ).
   lo_parser = lo_ixml->create_parser(
                          stream_factory = lo_streamfactory
                          istream        = lo_istream
                          document       = lo_document ).

   "This actually makes the XML document navigable
   lo_parser->parse( ).


Please need experts help .

Thanks & Regards,

Suji. 

Hi all,


I am trying to consume the demo Service Desk plus REST API with CL_HTTP_CLIENT in ABAP.

Demo Service Desk plus trail version and installed in my local PC.(Application run on localhost).

I got help document from Service Desk plus to consuming REST API and URL format as below.


http://<servername>:<port number>/sdpapi/request/

http://localhost:8080/sdpapi/request/

  1. The operation name "ADD_REQUEST" should be sent as a "POST attribute" with key "OPERATION_NAME"
  2. The technician key should be sent as a "POST attribute" with key "TECHNICIAN_KEY"
  3. Input is an XML string sent as "POST attribute" with key "INPUT_DATA". 


Based on the above document, I have prepared my URL below : URL:


http://localhost:8080/sdpapi/request/?OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=A2D2DEFC-C3BB-4F17-A...<Operation><Details><requester>Howard Stern</requester><subject>Help</subject><status>1</status><description>Everything is broken</description></Details></Operation>


When  running the above URL in browse it is working fine (Please see the attached jpg ).




But i am facing problem when try to consum in ABAP.  This due to API in localhost  ?


Error message : ( host: proxy, service: 8080 failed (NIEHOST_UNKNOWN)#RM-T37, U19433 )


Code Snap


  lv_service = 'http://localhost:8080/sdpapi/request/?OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=A2D2DEFC-C3BB-4F17-A...&INPUT_DATA=<Operation><Details><requester>Howard Stern</requester><subject>Help!</subject><description>Everything is broken</description></Details></Operation>'.
  

   cl_http_client=>create_by_url(
     EXPORTING
       url                = lv_service
     IMPORTING
       client             = lo_http_client
     EXCEPTIONS
       argument_not_found = 1
       plugin_not_active  = 2
       internal_error     = 3
       OTHERS             = 4 ).

   lo_http_client->send(
     EXCEPTIONS
       http_communication_failure = 1
       http_invalid_state         = 2 ).

   lo_http_client->receive(
     EXCEPTIONS
       http_communication_failure = 1
       http_invalid_state         = 2
       http_processing_failed     = 3 ).

   "Prepare XML structure
   CLEAR lv_result .
   lv_result = lo_http_client->response->get_cdata( ).
   lo_ixml = cl_ixml=>create( ).
   lo_streamfactory = lo_ixml->create_stream_factory( ).
   lo_istream = lo_streamfactory->create_istream_string(
                                    lv_result ).
   lo_document = lo_ixml->create_document( ).
   lo_parser = lo_ixml->create_parser(
                          stream_factory = lo_streamfactory
                          istream        = lo_istream
                          document       = lo_document ).

   "This actually makes the XML document navigable
   lo_parser->parse( ).


Please need experts help .

Thanks & Regards,

Suji. 

5 REPLIES 5
Read only

Former Member
0 Likes
3,673

Hello,

The "send" method sends the http requisition from sap server, is the sap server on your localhost too? if not it will not work. Try to call your IP on lv_service (ie: http://192.168.1.100/...) and make sure that the port 8080 is available.

If this doesn't work try the fm HTTP2_GET as below:

     CALL FUNCTION 'HTTP2_GET'

         EXPORTING

           absolute_uri          = ld_uri_use

           rfc_destination       = 'SAPHTTPA'

           blankstocrlf          = 'X'

           timeout               = 120

         TABLES

           response_headers      = lt_response_headers

           response_entity_body  = lt_response

         EXCEPTIONS

           connect_failed        = 1

           timeout               = 2

           internal_error        = 3

           tcpip_error           = 4

           data_error            = 5

           system_failure        = 6

           communication_failure = 7

           OTHERS                = 8.

(In this fm is important to pass the response_headers and response_entity_body or it will not work.

Read only

0 Likes
3,673

Hi Wolg Tadeo,

Thanks for the quick reply. The SAP Server is not on my local host. i have try to call url with my PC IP, still the same issue.

I used HTTP2_GET function module. but still no luck.


I passed empty tables to function module.

* http_load_data

*http_load_headers


url = 'http://localhost:8080/sdpapi/request/?OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=A2D2DEFC-C3BB-4F17-A...<Operation><Details><requester>H</requester><subject>H</subject><description>Ev</description>

</Details></Operation>'.


   CALL FUNCTION 'HTTP2_GET'
     EXPORTING
       absolute_uri                = url
       rfc_destination             = 'SAPHTTPA'
       blankstocrlf                = 'X'
       timeout                     = 120
     IMPORTING
       response_entity_body_length = length
     TABLES
       response_entity_body        = http_load_data
       response_headers            = http_load_headers
     EXCEPTIONS
       OTHERS                      = 1.

Read only

0 Likes
3,673

Hi,

If the SAP server is not on your localhost you must change the http://localhost:8080 in your URL to your local LAN IP (ie: http://your.ip:8080). Also access SM59 expand TCP/IP Connections folder and double click SAPHTTPA, then click in test connection and post the result here.


EDIT: And check if the port 8080 is available for lan connections.

Regards,

Wolg

Read only

0 Likes
3,673

Hi Wolg,

Connection test for SAPHTTPA below.

Demo Service Desk plus Proxy settings.

Port 8080 is available for lan connections"

Thanks,

Suji

Read only

0 Likes
3,673

Hello, I am a little out of time but soon I'll return to help you, this configuration is apparently wrong (but the SAPHTTPA is ok).

Regards,

Wolg