cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member

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.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Hi all,

I have solved the problem.The code is the following

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 = 'text/html'.

CONTENT_TYPE = 'multipart/form-data'.

CALL METHOD CLIENT->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = 'Accept'

VALUE = 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8'.

CALL METHOD CLIENT->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = 'Accept-Language'

VALUE = 'es-es,es;q=0.8,en-us;q=0.5,en;q=0.3'.

CALL METHOD CLIENT->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = 'Accept-Charset'

VALUE = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'.

CALL METHOD CLIENT->REQUEST->IF_HTTP_ENTITY~SET_FORMFIELD_ENCODING

EXPORTING

FORMFIELD_ENCODING = CL_HTTP_REQUEST=>IF_HTTP_ENTITY~CO_ENCODING_RAW .

        • form

data: it_formulario type TIHTTPNVP,

wa_formulario like line of it_formulario,

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;name="event"'.

CALL METHOD PART->APPEND_CDATA

EXPORTING

DATA = 'YO'.

part = CLIENT->REQUEST->IF_HTTP_ENTITY~ADD_MULTIPART( ).

CALL METHOD PART->SET_header_FIELD

EXPORTING

NAME ='content-disposition'

VALUE ='form-data;name="secret"'.

CALL METHOD PART->APPEND_CDATA

EXPORTING

DATA = 'XXXXX'.

part = CLIENT->REQUEST->IF_HTTP_ENTITY~ADD_MULTIPART( ).

CALL METHOD part->set_header_field

EXPORTING

name = 'content-disposition'

value = 'form-data; name="content"; filename="PaymentTerms.zip";'.

CALL METHOD PART->SET_CONTENT_TYPE

EXPORTING

CONTENT_TYPE = 'application/x-zip-compressed'.

data: it_data type xstring,

FICHERO(40) TYPE C,

len type i.

FICHERO = 'F:XXXXXXXsrcPaymentTerms.zip'.

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.

                • HACEMOS LA PETICIÓN

call method client->send

EXPORTING

timeout = 200

EXCEPTIONS

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

others = 4.

I hope it can be useful.

Former Member
0 Kudos

This post was of great help to me...

Former Member
0 Kudos

hello, I have been programming in ABAP for a long time but have never worked with HTTP. I am trying to do something similar to this but I can't find any information documenting how to determine the input values or what is needed. Every example I find is different and I can not find documentation to learn from. Where did you get information to begin learning this? I need to post to a web service as soon as possible and I can't figure out where to begin. Is there documentation somewhere? I'll buy a book if it exists.

amy_king
Active Contributor
0 Kudos

Thanks for posting your solution Pablo! I think this may help me with a similar requirement.

Cheers,

Amy

Former Member

Dear Pablo,

Many thanks for your solution. This helped me a lot today. Have a nice day!

Cheers,

Tamás

Answers (0)