Application Development and Automation 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: 
Read only

post CL_HTTP_CLIENT multipart/form-data error

abityildiz
Active Participant
6,888

hi all,

i am sending rest api post service.Post service is get the multipart/form-data.i just add 1 file.extension of file is .card.

How can i solve this problem?Can you help me please?

But get this error :


it is rest api result manuel :

it is my abap code :

 
CONCATENATE lv_access_token lv_connect_sid lv_userid INTO lv_value SEPARATED BY space.

  FREE lo_http_client.

  CALL METHOD cl_http_client=>create_by_url

    EXPORTING

      url                = lv_url

    IMPORTING

      client             = lo_http_client

    EXCEPTIONS

      argument_not_found = 1

      plugin_not_active  = 2

      internal_error     = 3

      OTHERS             = 4.



  IF sy-subrc <> 0.

    "error handling

  ENDIF.

  lo_http_client->request->set_method('POST').

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

  lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).

  lo_http_client->request->set_header_field( name = 'cookie' value = lv_value ).

  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.



  DATA:part TYPE REF TO if_http_entity.



  part = lo_http_client->request->add_multipart( ).

  part->set_content_type( content_type ='application/octet-stream' ).

  part->set_header_field( name  = if_http_header_fields=>content_disposition

                          value = 'form-data;name="card";filename="admin2.card"' ).



  DATA:filename TYPE string.

  filename = '/tmp/admin2.card'.



  DATA:lv_raw TYPE xstring.

  OPEN DATASET filename FOR INPUT  IN LEGACY BINARY MODE CODE PAGE '1100'.

  IF sy-subrc = 0.

    DO.

      CLEAR lv_raw.

      READ DATASET filename INTO lv_raw.

      IF sy-subrc EQ 0.

        CALL METHOD part->append_data

          EXPORTING

            data = lv_raw.

      ELSE.

        EXIT.

      ENDIF.

    ENDDO.

  ENDIF.

  CLOSE DATASET filename .



  CALL METHOD lo_http_client->send

    EXPORTING

      timeout                    = 200

    EXCEPTIONS

      http_communication_failure = 1

      http_invalid_state         = 2

      http_processing_failed     = 3

      OTHERS                     = 4.

  IF sy-subrc = 0.

    CALL METHOD lo_http_client->receive

      EXCEPTIONS

        http_communication_failure = 1

        http_invalid_state         = 2

        http_processing_failed     = 3

        OTHERS                     = 5.

  ENDIF.



  CALL METHOD lo_http_client->response->get_status

    IMPORTING

      code   = lv_http_code

      reason = lv_http_reason.

  IF lv_http_code = '204'.



  ENDIF.


it is my fiori code : it is work fine.
onCardImport: function(oEvent) {


			var fU = this.getView().byId("fileUploader");
			var domRef = fU.getFocusDomRef();
			var file = domRef.files[0];


			var xhr = new XMLHttpRequest();
			var fd = new FormData();
			var file = new File([file], file.name, {
				"type": "application/octet-stream",
				"lastModified": Date.now()
			});


			xhr.open("POST", "url:3000/api/wallet/import");
			xhr.onreadystatechange = function() {
				if (xhr.readyState == 4 && xhr.status == 200) {
					alert(JSON.parse(xhr.responseText));
				}
			};
			fd.append("card", file);  
			xhr.withCredentials = true;		 
			xhr.send(fd);


		}

1 ACCEPTED SOLUTION
Read only

abityildiz
Active Participant
0 Likes
3,025

i solved.

i added new field in the header fields.

lo_http_client->request->set_header_field( name = 'X-Access-Token' value = lv_token ).

1 REPLY 1
Read only

abityildiz
Active Participant
0 Likes
3,026

i solved.

i added new field in the header fields.

lo_http_client->request->set_header_field( name = 'X-Access-Token' value = lv_token ).