Skip to Content
0
Jun 10 at 06:49 PM

Consume POST REST Web Service in ABAP to send PDF in base64 format.

166 Views

Hello experts.

I'm trying to consume a POST webservice in ABAP.

The communication is working, since if I don't send the base64 string, I get a webservice message that base64 string is required. But when I add the base64 string, I get the error "500 Network Connection Broken Connection closed"

When I run the same JSON request on POSTMAN, it works fine with the PDF base64 string.


Tracing the execution in SMICM, I get the following error(even when I get the response from the web service):

For testing, I've made a program where I can upload the file, use a hardcode string or use none.

Below is a part of the code:

 lv_url = 'https://server/service'.

  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url           = lv_url
      proxy_service = '7000'
    IMPORTING
      client        = lo_http_client.

  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.

  lo_http_client->set_compression( if_http_request=>co_compress_in_all_cases ).
  lo_http_client->request->set_method( if_http_request=>co_request_method_post ).
  lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).

  lv_uri = '/webapi/api/v1/aaa/service'.

  cl_http_utility=>set_request_uri( request = lo_http_client->request
                                    uri     = lv_uri ).


  DATA(pdf64) = cl_http_utility=>encode_x_base64( unencoded = pdf_xstring ).

  lv_json = '{'
  && '"IdDistribucionCedible": 0,"Doc_Cenabast": 111111111,"Rut_Proveedor": "22222222222",'
  && |"Documento":"{ COND #( WHEN p_fixed EQ 'X' THEN 
* HERE IS THE START OF THE BASE64 STRING IT'S A REALLY LONG STRING.
|JVBERi0xLjcKCjQgMCBvYmoKPDwKL0ZpbHRlciAvRmxhdGVEZWNvZGUKL0xlbmd0aCA5MzQ2|
  && |Cj4+CnN0cmVhbQp4nI1dPW5uu27tzyhOHSCG/rU1jFdlAA9IUtgBXuZfRNQm11qyXeRW36V0|
  && |MTggMDAwMDAgbg0KdHJhaWxlcgo8PAovSW5mbyA2IDAgUgovUm9vdCAxIDAgUgovU2l6ZSA3|
  && |Cj4+CnN0YXJ0eHJlZgo5OTA2CiUlRU9GCg==| 
* HERE IS THE END OF THE BASE64 STRING.
WHEN p_path EQ 'X' THEN pdf64 ELSE ' ' ) }",|
  && '"FechaCreacion": "12-05-2021 14:06:14","FechaActualizacion": "12-05-2021 14:06:14" }'.


 cl_demo_output=>display_text( lv_json ).

 lo_http_client->request->set_data( cl_bcs_convert=>string_to_xstring( lv_json) ).

  DATA(lv_len) = strlen( lv_json ).

  CALL METHOD lo_http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Length'
      value = |{ lv_len }|.

  CALL METHOD lo_http_client->request->set_header_field
    EXPORTING
      name  = 'Authorization'
      value = lv_response_str.

  CALL METHOD lo_http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Type'
      value = 'application/json; charset=utf-8'.

  CALL METHOD lo_http_client->request->set_header_field
    EXPORTING
      name  = 'Connection'
      value = 'Keep-Alive'.


  CALL METHOD lo_http_client->request->set_header_field
    EXPORTING
      name  = 'Host'
      value = 'servicehost'.


  CALL METHOD lo_http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.

  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.

Attachments

tywaq.png (41.1 kB)