Sending Data in a URL link (Consuming API from Outside in ABAP)
2023-11-2 04:59:17 Author: blogs.sap.com(查看原文) 阅读量:6 收藏

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.


文章来源: https://blogs.sap.com/2023/11/01/sending-data-in-a-url-link-consuming-api-from-outside-in-abap/
如有侵权请联系:admin#unsafe.sh