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: 

HTTP_POST Multipart - Send files to external service

Former Member
0 Kudos

In Argentina, we had a legal requirement to send a text file using http_post, as a multipart form.

We need to know how we cand send 3 parameters at once (User, password, text file ).

HTML code solves file sent , but we need to develop an application directly on SAP, using ABAP code, without XI-PI.

We reach external service, but IT can not receive attached file, we assume that HTTP_POST method, RFC, etc (we tried all of them) add some lines or characters that are imposible to external service to read.

Does anyone solve it?

8 REPLIES 8

asdasd_asdasd
Active Participant
0 Kudos

The idea is "simple":

  1. Use the class "cl_http_client" for creating the http client.
  2. Set the http header fields
  3. Build the http request manually with the 3 parameters with multipart format
  4. Set the request on the client you built manually, and send

Best Regards.

0 Kudos

Hello all, I'm also having problems with this service so I'll join the discussion.

Maxi, I arrived at the same steps you mention in my coding, but still I'm getting the following response:

<?xml version='1.0' encoding='ISO-8859-1'?><TBError>

  <tipoError>ERROR INESPERADO</tipoError>

  <codigoError>01</codigoError>

  <mensajeError>[Número de Transacción: XXXXXX]. Reportar  al Sector Seguridad.</mensajeError>

</TBError>

My guess is that either some additional field needs to be set, or else the service does not allow HTTP (as opposed to HTTPS) based calls not made from the provided JSP page, even in testing environment.

Here's the essential coding, since you made it work, could you remark any missing or incorrect lines?

********************************* Crear Cliente
  cl_http_client=>create(
    EXPORTING
      host          = 'cot.test.arba.gov.ar'
      service       = '80'
      scheme        = cl_http_client=>schemetype_http
    IMPORTING
      client        = client ).

********************************* Header fields
  client->request->set_method( if_http_request=>co_request_method_post ).
  client->request->set_version( if_http_request=>co_protocol_version_1_1 ).
  cl_http_utility=>set_request_uri( request = client->request
                                    uri     = '/TransporteBienes/SeguridadCliente/presentarRemitos.do').

  client->request->set_content_type( content_type = 'multipart/form-data'  ).

********************************* USER
  multipart = client->request->add_multipart( ).

  multipart->set_header_field(
      name   = if_http_header_fields=>content_disposition
      value  = 'form-data; name="user"'
         ).

  multipart->suppress_content_type( ).

  multipart->set_cdata(
      data   = p_user
         ).

********************************* PASSWORD
  multipart = client->request->add_multipart( ).

  multipart->set_header_field(
      name   = if_http_header_fields=>content_disposition
      value  = 'form-data; name="password"'
         ).

  multipart->suppress_content_type( ).

  multipart->set_cdata(
      data   = p_pass
         ).

********************************* FILE
  multipart = client->request->add_multipart( ).

  g_file1_form_data = 'form-data; name="file"; filename="&"'.
  REPLACE FIRST OCCURRENCE OF '&' IN g_file1_form_data WITH g_file1_nombre.   "TB_XXXXXXXXXXX_...

  multipart->set_header_field(
      name   = if_http_header_fields=>content_disposition
      value  = g_file1_form_data
         ).

  multipart->set_content_type( 'text/plain' ).

  multipart->set_cdata(
      data   = g_file1_cont    "01|XXXXXXXXXXX.... sep: cl_abap_char_utilities=>cr_lf

         ).

********************************* Enviar Request
  client->send( ).
  client->receive( ).

********************************* Obtener y procesar Response
  g_bin = client->response->get_data( ).
  client->close( ).

  CALL FUNCTION 'ECATT_DISPLAY_DOM_OR_XSTRING'
    EXPORTING
      im_xstring = g_bin.

Many thanks and regards.

0 Kudos

Very, very far from the solution...

0 Kudos

It would be great if you shared it then.

0 Kudos

This solution is sold as a package, so I can not paste code,please contact maxi1555@gmail.com

0 Kudos

Hi Alejandro,

We are working on this solution, without using XI-PI. We are very close.

We create a manual post, but SAP is adding some characters that are imposible to read for external service.

Could you please send me your email?

Thanks,

Daniela.

alejandro_bindi
Active Contributor
0 Kudos

I prefer not to post my email here. I'm now following you both so you can send me private messages. If you want, please do the same so that I can send you as well.

Regarding the subject, I'm actually trying to set up this interface using PI 7.3. But the direct ABAP approach was a last resort measure in case I couldn't get it to work on PI (see http://scn.sap.com/thread/3262537), so I'm still interested in making it work.

Regarding my coding, I analyzed the request (using ICF recorder) and compared it with the generated from the JSP page (using fiddler) and saw almost no differences. So I'm puzzled to hear I'm so far from the solution. Maxi, without posting the coding, can you please explain the differences?

Daniela, are you sure the problem are the characters? How did you arrive at this conclusion? In that case, maybe it's an encoding issue...

0 Kudos

I made it work using the older HTTP_POST function module, below I posted the needed code. Although a better solution would be to use the newer IF_HTTP interfaces, this works:


* Datos de conexión
  l_url   = 'http://cot.test.arba.gov.ar/TransporteBienes/SeguridadCliente/presentarRemitos.do'.
  l_proxy = 'XXX.XXX.XXX.XXX:XXXX'. "Proxy para salida a internet (IP:PUERTO)

* Header
  APPEND 'Content-Type: multipart/form-data; boundary=---------------------------985513245641212'
      TO lt_request_headers.

* Cuerpo multipart
  l_file_cont_disp = 'Content-Disposition: form-data; name="file"; filename="&"'.
  REPLACE FIRST OCCURRENCE OF '&' IN l_file_cont_disp WITH u_file_name.
  SPLIT u_file_cont AT cl_abap_char_utilities=>cr_lf INTO TABLE lt_file_cont.

  APPEND: '-----------------------------985513245641212'      TO lt_request,
          'Content-Disposition: form-data; name="user"'       TO lt_request,
          space                                               TO lt_request,
          u_user                                              TO lt_request,
          '-----------------------------985513245641212'      TO lt_request,
          'Content-Disposition: form-data; name="password"'   TO lt_request,
          space                                               TO lt_request,
          u_pass                                              TO lt_request,
          '-----------------------------985513245641212'      TO lt_request,
          l_file_cont_disp                                    TO lt_request,
          'Content-Type: text/plain'                          TO lt_request,
          space                                               TO lt_request,
          LINES OF lt_file_cont                               TO lt_request,
          '-----------------------------985513245641212--'    TO lt_request.

* Enviar
  CALL FUNCTION 'HTTP_POST'
    EXPORTING
      absolute_uri          = l_url
      proxy                 = l_proxy
      blankstocrlf          = 'X'
    IMPORTING
      status_code           = l_status_code
      status_text           = l_status_text
    TABLES
      request_entity_body   = lt_request
      response_entity_body  = lt_response
      response_headers      = lt_response_headers
      request_headers       = lt_request_headers
    EXCEPTIONS
      connect_failed        = 1
      timeout               = 2
      internal_error        = 3
      tcpip_error           = 4
      system_failure        = 5
      communication_failure = 6
      OTHERS                = 7.