Application Development 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 from a URL

madhura_lobo
Advisor
Advisor
0 Kudos
679

Hi,

The need is to read a file from a URL and parse it. The class CL_HTTP_REQUEST and CL_IXML is supposed to be used for the same.

The URL is like https://abc.com/def . when we access the URL a txt file is read in the browser. we wish to programatically read this file in ABAP.

It will be very much appreciated if any of you can share with us in case you have done something like this.

Regards,

Madhura

1 ACCEPTED SOLUTION

JerryWang
Product and Topic Expert
Product and Topic Expert
0 Kudos
383

Hello Lobo,

you can refer to the code below:

INITIALIZATION.
  DATA : url_ TYPE string,
        http_client TYPE REF TO if_http_client,
        xbuffer TYPE xstring.

START-OF-SELECTION.
  url_ = 'http://bluepages.ibm.com'.
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url    = url_
    IMPORTING
      client = http_client
    EXCEPTIONS
      OTHERS = 1.
  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = '~request_uri'
      value = '/BpHttpApis/slaphapi?ibmperson/((sn=Chow)(c=cn)).list/byxml'.
  CALL METHOD http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
  WRITE / sy-subrc.
  CALL METHOD http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
  IF sy-subrc <> 0.
    WRITE / sy-subrc.
    EXIT.
  ENDIF.

END-OF-SELECTION.
  xbuffer = http_client->response->get_data( ).
  WRITE : / xbuffer.

Best Regards.

Jerry

2 REPLIES 2

JerryWang
Product and Topic Expert
Product and Topic Expert
0 Kudos
384

Hello Lobo,

you can refer to the code below:

INITIALIZATION.
  DATA : url_ TYPE string,
        http_client TYPE REF TO if_http_client,
        xbuffer TYPE xstring.

START-OF-SELECTION.
  url_ = 'http://bluepages.ibm.com'.
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url    = url_
    IMPORTING
      client = http_client
    EXCEPTIONS
      OTHERS = 1.
  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = '~request_uri'
      value = '/BpHttpApis/slaphapi?ibmperson/((sn=Chow)(c=cn)).list/byxml'.
  CALL METHOD http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
  WRITE / sy-subrc.
  CALL METHOD http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
  IF sy-subrc <> 0.
    WRITE / sy-subrc.
    EXIT.
  ENDIF.

END-OF-SELECTION.
  xbuffer = http_client->response->get_data( ).
  WRITE : / xbuffer.

Best Regards.

Jerry

0 Kudos
383

Hi Jerry,

Thanks for the response. Was helpful.

But the url is https, and i am getting Native SSL error. Any idea how to get this solved.

I added the following calls in addition to what you have specified above

http_client->send_sap_logon_ticket( ).

http_client->request->set_header_field( EXPORTING name = '~request_method' "#EC NOTEXT

value = 'GET' ).

cl_http_utility=>set_request_uri( request = http_client->request

uri = url_ ).

http_client->response->get_status( IMPORTING code = lv_status

reason = lv_reason ).

Also specified the sap_username and sap_client in the create_by_url() method.

But the lv_status = 500 and lv_reason = Native SSL error.

Regards,

Madhura