Skip to Content
Technical Articles
Author's profile photo Srividya Tamilmani

Sending Data in a URL link (Consuming API from Outside in ABAP)

Recently I got a requirement to send a pdf file via a soap API, as this was new to me had to do some research and finally achieved it with the below given method. Posting this for the sake of people who are looking for a solution for this kind of a problem statement.

Requirement :

Send one pdf file as hexadecimal and invoice details in a given URL (API).

Solution :

This can be achieved in ABAP through some classes and methods in the report program itself. No need to create OData or any other complicated programs.

Steps :

Create a internal table of the data format to be send to client side.

TYPES : BEGIN OF ty_file,
text TYPE string,
END OF ty_file.

DATA : lt_file TYPE TABLE OF ty_file,
ls_file TYPE ty_file.

ls_file-text = '<soap> '.

append ls_file to lt_file.

ls_file-text = '<text1> Testing text </text1> '.

append ls_file to lt_file.

ls_file-text = '<text2> Testing Data 2 </text2> '.

append ls_file to lt_file.

ls_file-text = '</soap> '.

append ls_file to lt_file.

Now write the coding to send this data in the given URL.

 

  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = 'https://sample.url.link?WSDL' 
"Sample link but link ending with WSDL and starting with https.
    IMPORTING
      client             = o_http_client1
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      pse_not_found      = 4
      pse_not_distrib    = 5
      pse_errors         = 6
      OTHERS             = 7.

  IF sy-subrc <> 0.
  ENDIF.

Once u get the subrc zero here. set the header field values.

 o_http_client1->propertytype_logon_popup = o_http_client1->co_disabled.
  o_http_client1->propertytype_accept_cookie = if_http_client=>co_enabled.
  o_http_client1->request->set_method( 'POST' ).

  "Set Header fields
  CALL METHOD o_http_client1->request->set_header_field
    EXPORTING
      name  = '~request_method'
      value = 'POST'.

  CALL METHOD o_http_client1->request->set_header_field
    EXPORTING
      name  = 'Content-Type'
      value = 'text/xml'.

  CALL METHOD o_http_client1->request->set_header_field
    EXPORTING
      name  = 'Connection'
      value = 'keep-alive'.

All set to share our data now. Make a String or xstring of our internal table.

  DATA(lo_http_request) = o_http_client1->request.
 
  DATA : lv_str TYPE string.
  LOOP AT lt_file INTO DATA(wa).
    CONCATENATE lv_str wa-text INTO lv_str.
    CLEAR wa.
  ENDLOOP.

The file is ready as a string to send it in the url. lv_str.

 CALL METHOD lo_http_request->append_cdata
    EXPORTING
      data = lv_str.

We get more methods to set the values here, append_cdata() or append_data().

The type should be string or xstring.

  "Send data
  CALL METHOD o_http_client1->send
    EXCEPTIONS
      http_communication_failure = 1                  " Communication Error
      http_invalid_state         = 2                  " Invalid state
      http_processing_failed     = 3                  " Error when processing method
      http_invalid_timeout       = 4                  " Invalid Time Entry
      OTHERS                     = 5.

 

The values are sent now, Check the error and response from the client side now.

 DATA errortext TYPE string.
  CALL METHOD o_http_client1->get_last_error
    IMPORTING
      code    = subrc
      message = errortext.

    CALL METHOD o_http_client1->receive
    EXCEPTIONS
      http_communication_failure = 1                " Communication Error
      http_invalid_state         = 2                " Invalid state
      http_processing_failed     = 3                " Error when processing method
      OTHERS                     = 4.

  "Get the response
  CALL METHOD o_http_client1->response->get_status
    IMPORTING
      code   = gv_http_status_code                " HTTP Status Code
      reason = gv_http_status_text.               " HTTP status description

To show the response you have received here, we can theย  show_html method or can download the response as a file in our local system. Here I am calling the show_html method to show the response.

DATA(result) = o_http_client1->response->get_cdata( ).

  IF result IS NOT INITIAL.
    cl_abap_browser=>show_html(
              EXPORTING
                title        = 'Status'
                size         = cl_abap_browser=>medium
                modal        = abap_true
                html_string  = result
                printing     = abap_false
                format       = cl_abap_browser=>landscape
                position     = cl_abap_browser=>topleft
                context_menu = abap_false
                check_html   = abap_true
                dialog       = abap_true
            ).
Endif.

 

Thanks for reading, this is working for me. Feel free to give feedback or a better solution if you have done this earlier.

 

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Matthew Billingham
      Matthew Billingham

      Why are you using CALL METHOD instead of the functional form - which you obviously know how to use since your last bit of code uses it?

      E.g. using

      CALL METHOD o_http_client1->receive
          EXCEPTIONS
            http_communication_failure = 1                " Communication Error
            http_invalid_state         = 2                " Invalid state
            http_processing_failed     = 3                " Error when processing method
            OTHERS                     = 4.

      instead of

      o_http_client1->receive(
          EXCEPTIONS
            http_communication_failure = 1                " Communication Error
            http_invalid_state         = 2                " Invalid state
            http_processing_failed     = 3                " Error when processing method
            OTHERS                     = 4 ).

       

       

      Author's profile photo Ulrich Schmidt
      Ulrich Schmidt

      The question rather is: why does ABAP always have several different ways of doing the same thing...?

      ๐Ÿ™‚

      Author's profile photo Shai Sinai
      Shai Sinai

      I guess it's called backward compatibility.

      Author's profile photo Ulrich Schmidt
      Ulrich Schmidt

      Yep, that must be it... ๐Ÿ™‚

      Author's profile photo Matthew Billingham
      Matthew Billingham

      Because they didn't get the syntax right first time!

      Author's profile photo Ulrich Schmidt
      Ulrich Schmidt

      ABAP has a syntax?

      Author's profile photo Ulrich Schmidt
      Ulrich Schmidt

      I am not quite sure, what you mean by the expression "Sending Data in a URL link"? A URL is basically an "address". You cannot send something "in an address", you can only send something "to an address"?!

      The only case, where there is actually data in a URL, is the case of CGI ("Common Gateway Interface"), which is basically a technology of the 90s to send data to a server in an HTTP GET request (which by definition does not have a body).

      But in your case, you are using HTTP POST, so the request has a body, and the data is sent in the body and not in the URL...