Skip to Content
1
Former Member
Mar 29, 2010 at 02:48 PM

ABAP: send file with http post multipart/form-data

10824 Views

Hi all,

I have a problem sending a file from an http request. What I want to execute from ABAP is this html code:

<form method="post" enctype="multipart/form-data" action="http://xx.xx.xxxxx/test/fileupload?option=xxx">
user:<input type="text" name="event" value="yo"><br>
Password: <input type="password" name="secret" value="XXXXXXXX"><br>
File:<input type="file" name="content"><br>
<input type=submit value="Submit">
</form>

Tahts what I've done:

host = 'http://xx.xx.xxxxx/test/fileupload?option=xxx'.
data l_respuesta TYPE string.
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL                = host
IMPORTING
CLIENT            = client
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
PLUGIN_NOT_ACTIVE       = 2
INTERNAL_ERROR            = 3
others                                = 4
.

****** DATOS DE LA PETICIÓN
* set http method POST
    call method client->request->set_method(
      if_http_request=>CO_REQUEST_METHOD_POST ).

* set protocol version
    client->request->set_version(
         if_http_request=>co_protocol_version_1_0 ).

* content type
    CALL METHOD CLIENT->REQUEST->IF_HTTP_ENTITY~SET_CONTENT_TYPE
      EXPORTING
        CONTENT_TYPE = 'multipart/form-data'.

**** formulario
    data: it_formulario type TIHTTPNVP,
          wa_formulario like line of it_formulario.

    wa_formulario-name  = 'event'.
    wa_formulario-value = 'yo'.
    append wa_formulario to it_formulario.
    clear wa_formulario.
    wa_formulario-name  = 'secret'.
    wa_formulario-value = 'XXXXXXXX'.
    append wa_formulario to it_formulario.
    clear wa_formulario.
    wa_formulario-name  = file'.
    wa_formulario-value = 'MC.exe'.
    append wa_formulario to it_formulario.
   

    client->request->set_form_fields( FIELDS = it_formulario ).

   call method client->send
     EXPORTING
       timeout                                 = 8
     EXCEPTIONS
       http_communication_failure = 1
       http_invalid_state                 = 2
       http_processing_failed         = 3
       others                                   = 4.

   if sy-subrc <> 0.
     call method client->get_last_error
       IMPORTING
         code    = subrc
         message = errortext.
   endif.

    Data: part type ref to if_http_entity.
    part =  CLIENT->REQUEST->IF_HTTP_ENTITY~ADD_MULTIPART( ).

    CALL METHOD part->set_header_field
      EXPORTING
        name  = 'content-disposition'
        value = 'form-data;type="file" name="content"; filename="MT.exe";'.

    CALL METHOD part->set_header_field
      EXPORTING
      name = 'content-type'
      value = 'bin'.

    data: it_data type xstring,
          FICHERO(40) TYPE C,
          len type i.

    FICHERO = 'F:XXXXXXXexeMT.EXE'.
    OPEN DATASET fichero FOR INPUT IN BINARY MODE.
    read dataset fichero into it_data.

    len = xstrlen( it_data ).

    CALL METHOD part->set_data
      EXPORTING
        data   = it_data
        offset = 0
        length = len.

The problem, is that I get the response: "Throwable caught: Illegal form post || No stack available. "

Thats the first time I work on this and I havent found many examples.... If you could send some example it will be very helpful.

Thanks for your time.